PHP: $_SESSION destroyed after page reload or refresh


Staff member
My session is not working correctly, its unset after when I reload the url, but not when I refresh the page using browser refresh button, it sent again form information to browser , says <strong>Confirm Form Resubmission</strong> and session still remain, but when I press enter in the browser url, the session gone.

here is my Code

<strong>UPDATED and complete php fil code</strong>

&lt;?php session_start();
error_reporting(E_ALL ^ E_NOTICE);
require_once 'protect.php';
$logout = $_GET['logout'];

&lt;!doctype html&gt;
&lt;script src="" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src=""&gt;&lt;/script&gt;&lt;script&gt; google.load("jquery",'1.7'); google.load("jqueryui", "1"); &lt;/script&gt;
&lt;script src="js/mutate/"&gt;&lt;/script&gt;
&lt;script src="js/mutate/mutate.min.js"&gt;&lt;/script&gt;

&lt;link href="//" rel="stylesheet"&gt;
&lt;link rel="stylesheet" type="text/css" href="css/animate.min.css"&gt;
&lt;script src="js/signinout.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="js/send_message.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="js/refresh_message_log.js" type="text/javascript"&gt;&lt;/script&gt;

$('.main').mutate('scrollHeight', function(el) {
        expanded = false;
        $('#scrollHeight').text('scrollHeight changed:' + $('.main').prop("scrollHeight"));
        $('.main').stop().animate({scrollTop:$('.main')[0].scrollHeight}, 1000);

function logout() {
&lt;meta charset="utf-8"&gt;
&lt;title&gt;Live Chat&lt;/title&gt;

if ($_POST['signIn']) {
$username = $_POST['username'];
$_SESSION['username'] = $username;
require_once 'cn.php';
    if ($username) {
        $query = "SELECT * FROM users WHERE username='$username'";
        $result = mysqli_query($cn,$query);
        $numrows = mysqli_num_rows($result);
        if ($numrows == 1) {
            $row = mysqli_fetch_assoc($result);
            $dbId = $row['id'];
            $dbadmin = $row['username'];
            if ($username == $dbadmin) {
                $_SESSION['adminid'] = $dbId;
                $_SESSION['admin'] = $dbadmin;
        else {
            echo 'No user found.';
        $AdminId = $_SESSION['adminid'];
        $Admin = $_SESSION['admin'];
        $welcome =  'Welcome &lt;span id="loggedUser"&gt;'.$Admin.'&lt;/span&gt;!';

if ($logout == 'admin') {

    &lt;div class="chatBox"&gt;
        &lt;div class="user"&gt;
        &lt;div class="chatlogo"&gt;Admin Chat&lt;/div&gt;
        &lt;?php if (!$Admin &amp;&amp; !$AdminId):?&gt;
        &lt;form name="SignIn" id="signInForm" action="./adminChat.php" method="post" onSubmit=""&gt;
            &lt;span class="error animated"&gt;Invalid uername&lt;/span&gt;

            &lt;input name="username" type="text" id="username" placeholder="Enter username" size="13px" onClick='document.username.value = "" '&gt;
            &lt;input name="signIn" type="submit" id="signIn" value="SING IN"&gt;
            &lt;?php else:?&gt;
            &lt;form action="signout_delete_content.php" method="post" onSubmit=""&gt;
            &lt;span class="welcome"&gt;&lt;?php echo $welcome;?&gt;&lt;/span&gt;
            &lt;input type="submit" value="SIGN OUT" id="signOut" onClick="logout()"&gt;

         &lt;?php endif;?&gt;   
        &lt;div class="main" id="result"&gt;
        &lt;div class="messageBox"&gt;
        &lt;?php if (!$AdminId &amp;&amp; !$Admin):?&gt;
            &lt;form name="messageBoxSignInForm" id="messageBoxSignInForm" onSubmit="return false"&gt;
            &lt;input type="submit" id="messageBoxSignIn" value="Sign In to Enter Chat"&gt;
            &lt;?php else:?&gt;
          &lt;form name="newMessage" class="newMessage" action="" onSubmit="return false"&gt;
            &lt;textarea name="newMessageContent" id="newMessageContent" placeholder="Enter your message here."&gt;Enter your message here&lt;/textarea&gt;
            &lt;input type="submit" id="newMessageSend" value="Send"&gt;
            &lt;?php endif;?&gt;


if I put my this
$AdminId = $_SESSION['adminid'];
            $Admin = $_SESSION['admin'];
variables on the top of code with session_start()

the session isn't set on first time submit the form, you have to submit form again or just refresh the page.and also my
not working. it has function,

function logout() {

any one cane help me out in this situation?

any help would be appreciated.

many thanks in advance.