[英]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");
}
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']
而不是使用$_POST
从database
中SELECT
数据,然后像这样将其分配为session
$_SESSION['username'] = $row['username'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.