简体   繁体   English

PHP会话变量不持久

[英]PHP Session variables not persisting

I'm creating a site that uses sessions to store login details - I know its a bad security practice but that'll be dealt with later; 我正在创建一个使用会话存储登录详细信息的网站-我知道这是一种不良的安全做法,但稍后会进行处理; for now I just need to fix this issue. 现在,我只需要解决此问题。 Page 1 gets the login details from a login form and stores them in a session. 页面1从登录表单获取登录详细信息,并将其存储在会话中。 Page 2 is meant to display those variables, but I get these errors when trying to access the variables: 第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

Here is the code - I've left off the unrelated parts: Page 1 这是代码-我省略了不相关的部分:第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"];  
}

And page 2 where I'm getting the errors mentioned above: 第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"];

I've searched SO and have made sure there are no spaces or whatever before my session_start(); 我已经搜索了SO,并确保在session_start();之前没有空格或其他内容session_start(); By the way, if it helps my domain company that I'm using is One.com 顺便说一句,如果它对我正在使用的域名公司有所帮助,那就是One.com

$_SESSION["email"] and $_SESSION["password"] are set in the if , so in the case it is skipped you get this undefined indexes error (as elements with this indexes were not defined anywhere before).To get rid of this Notice you can use isset() function(also use it to validate $_POST user input). $_SESSION["email"]$_SESSION["password"]if中设置,因此在跳过它的情况下会出现此未定义索引错误(因为以前没有在任何地方定义具有该索引的元素)。这个的Notice ,你可以使用isset()功能(也用它来验证$_POST用户输入)。 Example : 范例:

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

PS Validating your POST input : PS验证您的POST输入:

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

Change condition to: 将条件更改为:

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

For full: 完整的:

Login.php Login.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: 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'];
}

I solved the problem. 我解决了问题。 When I was on one page I was on www.domain.com/page1 but when I was on the other page there was no www - domain.com/page2. 当我在一页上时,我在www.domain.com/page1上,但是当我在另一页上时,则没有www-domain.com/page2。 This meant that session variables were not persisting. 这意味着会话变量没有持久化。 To fox this, I need to edit the htaccess file to redirect to www if there is no www in the URL. 为了解决这个问题,如果URL中没有www,我需要编辑htaccess文件以重定向到www。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM