PHP重定向适用于Chrome浏览器,但不适用于其他浏览器
所以我试图通过一本书来学习自己的MVC我还处在一个非常基本的水平所以请记住,如果你愿意回答的话 这是我的应用程序布局的一部分: 现在我有了一个简单的登录表单PHP重定向适用于Chrome浏览器,但不适用于其他浏览器,php,forms,Php,Forms,所以我试图通过一本书来学习自己的MVC我还处在一个非常基本的水平所以请记住,如果你愿意回答的话 这是我的应用程序布局的一部分: 现在我有了一个简单的登录表单 <form method="post" action="index.php"> <input type="text" name="action" value="login" style="display: none"> <label for="email">Email&l
<form method="post" action="index.php">
<input type="text" name="action" value="login" style="display: none">
<label for="email">Email</label>
<input type="email" required="required" name="email" id="email" placeholder="Enter Your Email" />
<br />
<label for="password">Password</label>
<input type="password" required="required" name="password" id="password" placeholder="Enter Your Password" />
<button type="submit" name="submit">Login</button>
</form>
目录-用户
php服务器作为控制器。以下是“控制器”的部分摘录,是出现问题的地方
require_once('../config/db.php');
require_once('../model/users_db.php');
if(isset($_POST['action'])) {
$action = $_POST['action'];
if ($action == 'login') {
$email = htmlspecialchars($_POST['email']);
$pword = htmlspecialchars($_POST['password']);
$users = login($email, $pword);
if (is_array($users)) {
foreach ($users as $user) {
session_start();
$_SESSION['firstname'] = $user['firstname'];
$_SESSION['lastname'] = $user['lastname'];
$_SESSION['username'] = $user['username'];
$_SESSION['email'] = $user['email'];
$_SESSION['userType'] = $user['userType'];
$_SESSION['userID'] = $user['userID'];
header('Location:welcome.php');
die();
}
应用程序流程/步骤
action
值是否设置为登录exit()
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
flush();
header("Location: http://www.website.com/");
echo '<script>window.location.replace("http://www.example.com")</script>';
die('should have redirected by now');
错误报告(E|u ALL | E|u警告| E|u通知);
ini设置(“显示错误”,真);
冲洗();
标题(“位置:http://www.website.com/");
“echo”窗口。位置。替换(“http://www.example.com")';
死('现在应该已经重定向');
PHP重定向使用头代码通知浏览器重定向。但是,如果您的PHP代码在该头位置之前出现回声(甚至是警告),某些浏览器将不会重定向
在上面的代码中,它刷新所有内容并发送头位置。它还告诉浏览器使用javascript重定向,所以即使php头重定向不起作用,它也会起作用
希望对foreach-->中的end die()函数有所帮助,请将其删除,然后重试。请检查您的php代码是否没有错误,然后重试 在
:
例如:
header('Location:welcome.php')代码>除了重定向之外,该代码还有多个问题。
header
函数不起作用的原因是,header需要有一个绝对URL,如http://www.example.com/replace.php
,除此之外,位置:
标题名称和值之间需要有一个空格,如:
header('位置:http://www.example.com/replace.php)
请注意,为了使用header
函数设置响应头,之前不应发送任何头文件,如果在调用header
函数之前返回的空间很小,则该函数将不起作用
现在,让我们来谈谈这段代码的其余问题:
1-在开始使用$\u session superglobal之前,需要在页面顶部调用session\u start()
函数
2-在foreach循环中调用die()
,这意味着您的代码将只在数组中迭代一次,然后代码将停止。将die()
调用移到循环外部
3-使用filter\u val
函数()验证输入。我主要谈论的是电子邮件,但我认为您也可以将其应用于其他输入
4-不要以纯文本格式存储密码,请使用PHP提供的密码哈希API()可能还有另一种解决方案:
users_db.php
index.php
require_once('../config/db.php');
需要_once('../model/users_db.php');
如果(isset($_POST['action'])){
$action=$_POST['action'];
如果($action=='login'){
$email=htmlspecialchars($_POST['email']);
$pword=htmlspecialchars($_POST['password']);
$user=login($email,$pword);
如果($user!=null){
会话_start();
$\会话['firstname']=$user['firstname'];
$\会话['lastname']=$user['lastname'];
$\会话['username']=$user['username'];
$\会话['email']=$user['email'];
$\会话['userType']=$user['userType'];
$\会话['userID']=$user['userID'];
标题('Location:welcome.php');
“echo”窗口。位置。替换(“http://www.example.com")';
模具();
}
}
}
问题似乎出现在表单提交中,而不是设置会话后的重定向中。错误发生后,能否返回并尝试打印会话以查看所看到的内容。如果重定向有问题,那么会话应该已经设置好。请检查这篇文章,如果有帮助,首先检查你的文章是否正在执行。©Gijo,我认为我们需要找到一种方法,通过PHP本身来修复它。我们还需要找到这个问题的原因。再次感谢@Gijo,这仍然会导致相同的问题。我正在尝试检查其他原因,如果/当我可以获得更多信息时,将更新答案。问题不在于标题重定向代码。上面的一些代码是错误的。这就是原因。仔细检查你能在答案中添加ob_start()作为点吗。这可能是一个可能的问题,而不是用ob_start()
和ob_clean()
对代码进行黑客攻击,他应该追查罪犯(如果是这样的话)。是的,我同意。但是,是否不需要将ob_start()置于头重定向的顶部才能工作?不,设置头不需要启动输出缓冲区非常感谢您的持续帮助。不幸的是,这个解决方案会导致同样的问题
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
flush();
header("Location: http://www.website.com/");
echo '<script>window.location.replace("http://www.example.com")</script>';
die('should have redirected by now');
function login($email, $pword)
{
$sql = "SELECT * FROM users WHERE email = :email AND pword = :pword LIMIT 1";
$stmnt = $db->prepare($sql);
$stmnt->bindValue(':email', $email);
$stmnt->bindValue(':pword', $pword);
$stmnt->execute();
if ($stmnt->rowCount() > 0) {
return $stmnt->fetchAll();
} else {
return null;
}
}
require_once('../config/db.php');
require_once('../model/users_db.php');
if(isset($_POST['action'])) {
$action = $_POST['action'];
if ($action == 'login') {
$email = htmlspecialchars($_POST['email']);
$pword = htmlspecialchars($_POST['password']);
$user = login($email, $pword);
if ($user != null) {
session_start();
$_SESSION['firstname'] = $user['firstname'];
$_SESSION['lastname'] = $user['lastname'];
$_SESSION['username'] = $user['username'];
$_SESSION['email'] = $user['email'];
$_SESSION['userType'] = $user['userType'];
$_SESSION['userID'] = $user['userID'];
header('Location: welcome.php');
echo '<script>window.location.replace("http://www.example.com")</script>';
die();
}
}
}