【问题标题】:PHP session not destroying or unsettingPHP会话没有破坏或取消设置
【发布时间】:2014-12-31 05:38:34
【问题描述】:

我有以下 PHP 代码用于在我的 index.php 中检查登录

<?php
session_start();

$con = mysqli_connect("***", "***", "***", "***");

$fbid_check=$_SESSION['loginid_session'];
$fbphoto_session=$_SESSION['loginphoto_session'];

$fbname_sql=mysqli_query($con, "SELECT fb_name FROM uni_users WHERE fb_id='$fbid_check' ");
$name_fetch=mysqli_fetch_array($fbname_sql, MYSQLI_ASSOC);

$fbname_session=$name_fetch['fb_name'];

if(isset($fbname_session))
 {
    header("location: http://www.uniwink.com/landing/profile.php");
 }
mysqli_close($con);


?>

这会检查登录并重定向到 profile.php,该文件的标头中有以下 PHP 代码以检查登录

<?php
$con = mysqli_connect("****", "****", "****", "****");

session_start();
$fbid_check=$_SESSION['loginid_session'];
$fbphoto_session=$_SESSION['loginphoto_session'];

$fbname_sql=mysqli_query($con, "SELECT fb_name FROM uni_users WHERE fb_id='$fbid_check' ");
$name_fetch=mysqli_fetch_array($fbname_sql, MYSQLI_ASSOC);

$fbname_session=$name_fetch['fb_name'];

if(!isset($fbname_session))
{
   header("location: http://www.uniwink.com/landing");
}

mysqli_close($con);


?>

我有以下 logout.php,它是从 profile.php 调用的

<?php
session_start();
unset($_SESSION['loginid_session']);
unset($fbname_session);
session_destroy();
header("location: http://www.uniwink.com/landing");
exit();

?>

问题是注销后,它仍然进入 profile.php 。好像会话根本没有被破坏。问题是它直到昨天才正常工作并且突然发生了。谢谢

【问题讨论】:

  • 尝试移除 unset($fbname_session);它没有在注销页面上的任何地方定义

标签: php session web login session-variables


【解决方案1】:

尝试将session_start();移到mysql连接上方。

改变

$con = mysqli_connect("****", "****", "****", "****");
session_start();

session_start();
$con = mysqli_connect("****", "****", "****", "****");

也可以在会话中使用 isset

if (isset($_SESSION['loginid_session'])){ 
   .....
}

【讨论】:

  • 是的,我应该在会话中使用 if 条件而不是变量。谢谢:)
【解决方案2】:

尝试将以下内容添加到您的注销中;

$_SESSION = array();
if (ini_get("session.use_cookies"))
{
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

注意:这将破坏会话 - 不仅仅是会话数据。

【讨论】:

    【解决方案3】:

    检查会话。

    if (isset($_SESSION['loginid_session'])) 
    {
        header("location: http://www.uniwink.com/landing/profile.php");
    }
    else
    {
        header("location: http://www.uniwink.com/landing");
    }
    

    【讨论】:

    • 非常感谢。那行得通。我应该检查会话而不是变量。
    猜你喜欢
    • 2013-09-15
    • 1970-01-01
    • 2012-05-17
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    相关资源
    最近更新 更多