2014-07-17 15:10

PHP:页面重新加载或刷新后销毁$ _SESSION


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 Confirm Form Resubmission and session still remain, but when I press enter in the browser url, the session gone.

here is my Code

UPDATED and complete php fil code

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

<!doctype html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="http://www.google.com/jsapi"></script><script> google.load("jquery",'1.7'); google.load("jqueryui", "1"); </script>
<script src="js/mutate/mutate.events.js"></script>
<script src="js/mutate/mutate.min.js"></script>

<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/animate.min.css">
<script src="js/signinout.js" type="text/javascript"></script>
<script src="js/send_message.js" type="text/javascript"></script>
<script src="js/refresh_message_log.js" type="text/javascript"></script>

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

function logout() {
<meta charset="utf-8">
<title>Live Chat</title>

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 <span id="loggedUser">'.$Admin.'</span>!';

if ($logout == 'admin') {

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

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

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


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 onClick="logout()" 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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答