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

时间:2014-07-17 15:10:16

标签: php session

我的会话无法正常工作,在我重新加载网址后未设置,但是当我使用浏览器刷新按钮刷新页面时,它再次向浏览器发送表单信息,确认表单重新提交并且会话仍然存在,但是当我按下浏览器URL中的输入时,会话就消失了。

这是我的代码

更新并完成php fil代码

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

?>
<!doctype html>
<html>
<head>
<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>

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

function logout() {
    window.location.href="http://localhost/wordpress/wp-content/themes/onlinearn/chat/adminChat.php?logout=admin";
};
</script>
<meta charset="utf-8">
<title>Live Chat</title>
</head>

<body>
<?php
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') {
    //session_destroy();

}
?>
    <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()">
            </form>

        </form>
         <?php endif;?>   
        </div>
        <div class="main" id="result">
        </div>
        <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">
            </form>
            <?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;?>
          </form>
      </div>
    </div>

</body>
</html>

如果我使用session_start()

将我的$AdminId = $_SESSION['adminid']; $Admin = $_SESSION['admin'];变量放在代码顶部

会话未在第一次提交表单时设置,您必须再次提交表单或只刷新页面。而且我的onClick="logout()"无法正常工作。它有功能,

function logout() {
   window.location.href="http://localhost/wordpress/wp-content/themes/onlinearn/chat/adminChat.php?logout=admin";
};
在这种情况下,有没有一根手杖帮助我?

任何帮助将不胜感激。

提前多多感谢。

1 个答案:

答案 0 :(得分:1)

WordPress似乎没有调用session_start(),因为它希望无状态,如果定义了register_globals,它会自动销毁您的$_SESSION

在此处阅读更多内容:http://www.thinkingoutloud.co.za/content/20091012/php_wordpress_and_session

在这里你可以找到一个WP的插件,它可以使用会话变量:https://wordpress.stackexchange.com/questions/32646/how-to-use-my-own-custom-session-value-in-wordpress/72744#72744