【问题标题】:PHP Session variables not persistingPHP会话变量不持久
【发布时间】:2014-07-28 06:42:48
【问题描述】:

我正在创建一个使用会话来存储登录详细信息的站点 - 我知道这是一种不好的安全做法,但稍后会处理;现在我只需要解决这个问题。第 1 页从登录表单中获取登录详细信息并将其存储在会话中。第 2 页旨在显示这些变量,但在尝试访问变量时出现以下错误:

Notice: Undefined index: email in /customers/0/4/0/my-domain.com/httpd.www/upload-photo.php on line 10
Notice: Undefined index: password in /customers/0/4/0/my-domain.com/httpd.www/upload-photo.php on line 11

这里是代码 - 我省略了不相关的部分: 第 1 页

session_start();
var_dump($_SESSION);
ini_set('display_errors',1); 
error_reporting(E_ALL); 

// Just logged in
if($_POST["email"] != "" || $_POST["password"] != ""){
$email = strtolower($_POST["email"]);
$password = md5($_POST["password"]);

$_SESSION["email"] = $email;
$_SESSION["password"] = $password;

//echo "setting details from http post";
}else{
// Just redirected to this page
$email = $_SESSION["email"];
$password = $_SESSION["password"];  
}

第 2 页出现上述错误:

session_start();

ini_set('display_errors',1); 
error_reporting(E_ALL); 

var_dump($_SESSION);

$_SESSION["advert"] = $_GET['id'];
echo $_SESSION["email"];
echo $_SESSION["password"];

我已经搜索过 SO 并确保在我的 session_start(); 之前没有空格或其他任何内容 顺便说一句,如果它有助于我使用的域名公司是 One.com

【问题讨论】:

    标签: php session cookies session-variables session-cookies


    【解决方案1】:

    $_SESSION["email"]$_SESSION["password"] 设置在 if 中,因此在跳过它的情况下,您会收到此未定义索引错误(因为之前未定义具有此索引的元素)。摆脱此 @ 987654324@你可以使用isset()函数(也可以用它来验证$_POST用户输入)。示例:

    echo isset($_SESSION["email"]) ? $_SESSION["email"] : 'There is no element with key "email"';
    

    附:验证您的 POST 输入:

    if(isset($_POST["email"]) && isset($_POST["password"])){}
    

    【讨论】:

    • 抱歉,我该怎么做?
    • 下面这个答案呢,是不是和这个一样??
    • @Coder101 :下面的答案检查POST 输入,并且当if 被跳过时,你仍然会得到$_SESSION['email']Notice。所以你应该使用验证$_SESSION指定索引emailpassword
    • 好的,但我的问题不是我收到此通知,我的问题是我不知道为什么第 1 页的会话变量没有打印在第 2 页上。您的解决方案将摆脱通知,但仍然不会打印变量...如果这有意义:)
    • 嗯,当然不会。现在您可以从简单的调试开始:在 if 之前转储 $_POST 数组,看看它是否包含必要的信息(我猜它没有)。之后,您应该将错误追溯到表单提交或 ajax 调用,依此类推,直到您找出为什么 POST 什么也没给您发送。
    【解决方案2】:

    将条件改为:

    if(isset($_POST['email']) && isset($_POST['password'])){
      //do login
    }
    else {
        echo " Plese enter email and password";
    }
    

    完整版:

    登录.php

    <?php
    //here must be totaly clear, nothing can't be here
    session_start();
    
    if(isset($_POST['email']) && isset($_POST['password'])){
       $_SESSION['email'] = $_POST['email'];
       $_SESSION['password'] = $_POST['password'];
    }
    else {
       echo "Please enter email and password";
    }
    
    //here must be form to login
    

    Logged.php:

    <?php
    //here must be totaly clear, nothing can't be here
    session_start();
    
    if(!isset($_SESSION['email']) && !isset($_SESSION['password'])){
       header("Location: Login.php?err=Please login to use members area");
    }
    else {
       echo "You are logged in as:". $_SESSION['email'];
    }
    

    【讨论】:

      【解决方案3】:

      我解决了这个问题。当我在一个页面上时,我在 www.domain.com/page1 上,但是当我在另一页上时,没有 www - domain.com/page2。这意味着会话变量不会持续存在。为了解决这个问题,如果 URL 中没有 www,我需要编辑 htaccess 文件以重定向到 www。

      【讨论】:

        猜你喜欢
        • 2015-08-16
        • 1970-01-01
        • 1970-01-01
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 2013-04-23
        • 2021-04-09
        • 1970-01-01
        相关资源
        最近更新 更多