我正在为我的网站构建一个管理员登录页面。我确信我的脚本出了什么问题,但我必须登录两次才能重定向到管理员主页。有人能帮我什么地方做错了,或者我遗漏了什么吗?此外,我在localhost中尝试过,脚本运行良好,只需要登录一次。在我把它放进主机后,我必须登录两次。非常感谢。
<?php
session_start();
include_once("../config.php");
if(isset($_SESSION['name'])) {
header('location:viewallitems.php');
}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Login</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
if(isset($_POST['submit'])) {
$errors = array();
$required = array('email','pword');
foreach($required as $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errors[]= "The <strong> {$fieldname} </strong> was left blank";
}
}//End: foreach
if(empty($errors)) {
$email = mysqli_real_escape_string($mysqli,$_POST['email']);
$pword = mysqli_real_escape_string($mysqli,$_POST['pword']);
$hash_pw = $pword;
$query = "SELECT CONCAT_WS(' ', first_name, last_name)
AS name
FROM admin
WHERE email='$email'
AND pword='$hash_pw'
LIMIT 1";
$result = mysqli_query($mysqli,$query) or die(mysqli_error());
if(mysqli_num_rows($result) == 1) {
while($rows = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$_SESSION['name'] = $rows['name'];
header('location: viewallitems.php');
}
} else {
$errors[] = "The email or password do not match those on file.";
}
}
}//End if($_POST['submit'])
else {
if(isset($_GET['stat']) && $_GET['stat'] == 1) {
$message = "<ul><li>You are now logged out.</li></ul>";
}
}
?>
<div id="wrapper">
<div class="bg"><a href="../index.html" target="_blank"><img src="../images/logo.png" /></a></div>
<h1>Login To Admin Panel</h1>
<?php if(!empty($errors)) {
echo "<ul>";
foreach($errors as $error) {
echo "<li>{$error}</li>";
}
echo "</ul>";
}?>
<?php if(isset($message)) echo $message;?>
<form action="" method="post">
<center><img src="../images/avatar.png"></center>
<p>
<label for="email">Email:</label>
<input type="text" size="35" placeholder="Enter Email" name="email"/>
</p>
<p>
<label for="password">Password:</label>
<input type="password" size="35" placeholder="Enter Password" name="pword" />
</p>
<p>
<input class="blue" type="submit" name="submit" value="LOG IN" />
</p>
</form>
<div class='login'>
<a href="#">Login</a>
</div>
</div>
</body>
</html>
-
使用ob_start();
-
删除第四行前面的php。
-
将doctype更改为HTML5 doctype
-
在php之后,将HTML(从第7行开始)重新定位到新的位置。这可能会阻碍header()函数。不管怎样,把所有东西都放在一个地方总是很好的。
新增:
- 使用mysql_free_result($result)释放结果内存
当您重定向到viewallitems.php时,请在浏览器中检查url,登录时是否使用域前的www.infront,然后将其重定向到非www?因为php将www.yourdomain.com/viewallitems.php和yourdomain.com/view allitems.hp视为两个不同的域,因此会话不共享。如果您登录www.yourdomain.com/login.php,请尝试访问www.yourdomaincom/viewallitems.php,我认为您不需要再次登录。
如果这就是你的问题所在,我可以想出几个解决方案:
1.使用.htaccess文件重写模式,以便您网站中的所有页面都将重定向到域的www版本或非www版本。这样,当您输入yourdomain.com/login.php时,它将自动重定向到www.yourdomain.com/login.php
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
2.php中实际上有一个共享会话的函数,名为session_name和session_set_kokie_params。检查以下文档:
http://php.net/manual/en/function.session-name.php
http://php.net/manual/en/function.session-set-cookie-params.php
它过去是有效的,但现在不起作用了。我现在不太确定他们是否解决了这个问题。
3.为域www.yourdomain.com版本和非www.yourdomaincom版本设置会话。
我更喜欢使用第一个选项,因为它限制了浏览器将您的网站视为两个不同域的可能性。