【发布时间】:2018-11-07 20:48:59
【问题描述】:
我在php中无法启动session_start(),实在不明白是什么原因,试过论坛上有人建议的一些东西,但session_start()还是不行
没有检索变量的索引文件 $_SESSION:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Pagina1</title>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<article id="newPrincipal">
<h1>Usuário id:<?php echo $_SESSION['userId']; ?></h1>
</article>
<h1>Result:<?php echo "Usuário id:".$_SESSION['userId']; ?></h1>
</body>
</html>
<?php
echo '<pre>';
print_r($_SESSION['userId']);
echo '</pre>';
登录文件:
<?php
if(isset($_POST['login-submit'])){
require 'dbh.inc.php';
$users = $_POST['nome'];
$mailuid = $_POST['mailuid'];
$password = $_POST['pwd'];
$token;
if(empty($mailuid) || empty($password)){
header("Location: ../header.php=emptyfields");
exit();
}
else{
$sql = "SELECT * FROM users WHERE Usuarios=? AND email=?";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("Location: ../index.php?error=sqlerror");
exit();
}
else{
mysqli_stmt_bind_param($stmt, "ss", $mailuid, $users);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if($row = mysqli_fetch_assoc($result)){
$pwdCheck = password_verify($password, $row['pwdUsers']);
if($pwdCheck == false){
header("Location: ../header.php?error=wrongpwd");
exit();
}
else if($pwdCheck == true){
session_start();
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userId2'] = $row['uidUsers'];
$_SESSION['email'] = $row['emailUsers'];
header("Location: ../index.php?login=".$_SESSION['userId']);
}
else{
header("Location: ../login.php?error=wrongpwd");
exit();
}
}
}
}
}
else{
header("Location: ../index.php");
}
我使用的是外部服务器而不是 Xampp / Wamp,我不知道这是否意味着什么......
我尝试了各种方法在索引文件中使用 $ _SESSION 检索一些信息,但我不能。
【问题讨论】:
-
如果所有条件都满足,您的会话只会在第二段代码开始。启用错误报告并检查查询中的错误。
-
session_start();应始终位于代码的顶部 -
您的
index.php应该会生成一个错误,因为当它运行时,$_SESSION 中将没有任何内容,因为尚未完成登录。将ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);添加到脚本的顶部。这将强制任何 mysqli_ 错误生成您可以在浏览器上看到的异常以及正常的 PHP 错误 -
Riggsfolly 和 Funk 49 感谢您的回复。我做了你要求我做的一切,但它仍然没有工作,你知道找出代码中是否有任何错误的最佳方法吗??
标签: php html forms session login