简体   繁体   English

PHP会话将无法保存

[英]PHP session won't save

I've seen similar questions here but none of the solutions work for me. 我在这里看到了类似的问题,但是没有一种解决方案适合我。 I am running php 5.6 in WAMP and localhost. 我在WAMP和本地主机中运行php 5.6。 When the user logs in correctly, I start the session with session_start() and set $_SESSION['user'] to $_POST['username'] , and then i use header('Location: home.php') . 当用户正确登录时,我使用session_start()启动会话,并将$_SESSION['user']$_POST['username'] ,然后使用header('Location: home.php') home.php starts with session_start() function, and then i check if the $_SESSION['user'] is set. home.php从session_start()函数开始,然后检查$_SESSION['user']是否设置。 If it isn't, I redirect to the login page. 如果不是,我将重定向到登录页面。 However, even when I log in with valid password and email, it always redirects me to the login page for reasons unkown. 但是,即使我使用有效的密码和电子邮件登录,由于未知原因,它始终会将我重定向到登录页面。 I tried to echo the $_SESSION['user'] , but it just says that it's undefined. 我试图echo$_SESSION['user'] ,但是它只是说它是未定义的。 How do I fix this problem? 我该如何解决这个问题?

This is how I do it: 这是我的方法:

include('dbconnect.php'); //php with server connection detail
session_start();

$user_check = $_SESSION['login_user'];

$ses_sql = mysqli_query($db,"SELECT username FROM users WHERE username = '$user_check' ");

$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$login_session = $row['username'];

if(!isset($_SESSION['login_user'])){
    header("location:login.php");
}
  1. maybe you cannot set 'session_auto_start = on' in php.ini 也许您无法在php.ini中设置“ session_auto_start = on”
  2. First check the php.ini session_save_path in existence, and there is no comment out if there are general default in Widnows is c:\\windows\\tmp, the directory is not allowed to read other users by default.and Set folder for everyone to read writable 首先检查php.ini session_save_path是否存在,如果Widnows中的默认默认目录为c:\\ windows \\ tmp,则没有注释掉,默认情况下不允许该目录读取其他用户,并设置文件夹供所有人读取写

make sure your php.ini file have session.autostart = On 确保您的php.ini文件具有session.autostart = On

If you're using apache, and have the right Options, you can enable session.auto_start with a .htaccess file containing this line: php_flag session.auto_start 1 如果您使用的是Apache,并且具有正确的选项,则可以使用包含以下内容的.htaccess文件启用session.auto_start :php_flag session.auto_start 1

do session_start() on the top of everything. 在所有内容的顶部执行session_start()

We usually put session_start(); 我们通常把session_start(); at the header.php , The problem now is that you've assigned $_POST['username'] to a session , Which is wrong, We use the $_POST[''] to retrieve data from the database , So you have to type your SELECT query and get the username from username row inside the database then assign the row data to a session like this $_SESSION['username'] = $row['username']; header.php ,现在的问题是您已将$_POST['username']分配给一个session ,这是错误的,我们使用$_POST['']database检索数据,因此您必须输入您的SELECT查询并从database内的用户username row获取用户名,然后将该row数据分配给类似$_SESSION['username'] = $row['username'];的会话$_SESSION['username'] = $row['username'];

So in brief, Your mistake is using $_POST['username'] instead of SELECT ing the data from your database using $_POST then assigning it as session like this $_SESSION['username'] = $row['username']; 简而言之,您的错误是使用$_POST['username']而不是使用$_POSTdatabaseSELECT数据,然后像这样将其分配为session $_SESSION['username'] = $row['username'];

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

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