您好,我写代码,当我登录成功登录并转到我的愿望页面。但会议没有成功。我想用用户名更改登录按钮,但我失败了。请检查我弄错的地方。
<?php
start_session();
if(isset($_POST['login'])){
$user_email = $_POST['user_email'];
$user_pass = $_POST['user_pass'];
echo $user_email. $user_pass;
$sqlQuery= 'SELECT * FROM user WHERE user_email = :user_email';
$st= $conn->prepare($sqlQuery);
$st->execute(array('user_email' => $user_email));
while ($row = $st->fetch()){
$id =$row['id'];
$user_pass_hashed = $row['user_pass'];
$user_name = $row['user_name'];
if(password_verify($user_pass, $user_pass_hashed)){
$_SESSION['id'] = $id;
$_SESSION['user_name'];
header('location: index.php');
echo "<script type= 'text/javascript'>alert('Its Ok');</script>";
}else{
echo "<script type= 'text/javascript'>alert('Something went wrong');</script>";
}
}
}
?>
的index.php
<?php
start_session();
if(!isset($_SESSION['user_name']))
echo'<li><a href="login.php" class="btn btn-red">Log In</a></li>';
else
echo'<li><a href="admin.php" class="btn btn-red">Logged in</a></li>';
?>
答案 0 :(得分:2)
不是start_session
而是session_start()
;
但我担心为什么会这样做
<?php
start_session();
没有抛出某种错误?
更改为
<?php
session_start();
为防止php注意session already started
,
做:
<?php
if(!isset($_SESSION)){
session_start();//only start if none exists
}
?>
编辑: 改变这个
if(password_verify($user_pass, $user_pass_hashed)){
$_SESSION['id'] = $id;
$_SESSION['user_name'];//this is not set. you need to assign a value
header('location: index.php');
echo "<script type= 'text/javascript'>alert('Its Ok');</script>";
}
使用:
if(password_verify($user_pass, $user_pass_hashed)){
$_SESSION['id'] = $id;
$_SESSION['user_name'] = $user_name;//set current user's name
header('location: index.php');
echo "<script type= 'text/javascript'>alert('Its Ok');//header will override this echo.</script>";
}
您需要删除标题下方的echo
行。它永远不会被执行。另一种方法是将成功/失败的消息存储在会话变量中,然后输出,然后取消设置
答案 1 :(得分:0)
我认为一定是
<?php
if(!isset($_SESSION)){
session_start();
}
?>
你设置了一个$ _SESSION ['user_name'],但是你没有提供任何数据。
$ _ SESSION ['id'] = $ id; $ _SESSION [ 'USER_NAME'];
如果您只是做
,可以查看会话中的内容echo '<pre>;
print_r($_SESSION);
echo '</pre>;
请参阅Print_r了解更多