PHP登录不起作用-用户登录后仍然可以在受保护的页面上看到登录表单


PHP login not working - user still sees the login form on protected pages after logging in

------已解决------大家好,我现在已经解决了这个问题,正是我缺乏经验和聪明导致了这个问题。正如你从下面的评论中看到的,这个问题在我的.htaccess文件中。我已经放了RewriteRule ^admin adminlogin.php,所以这是将任何包含admin的页面改回adminlogin.php

------原始问题------我正在尝试在一个网站上使用一个简单的登录脚本。它是用php编码的,如下所示:

adminlogin:

<div class="login">
<form name="form1" method="post" action="checklogin.php">
<table width="379px" border="0px" cellpadding="3px" cellspacing="1px">
<tr>
<td colspan="3"><strong>Admin Login</strong></td>
</tr>
<tr>
<td width="78px">Username</td>
<td width="6px">:</td>
<td width="294px"><input name="myusername" type="text"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</div>

checklogin.php:

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="Logins"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die(mysql_error());
// username and password sent from form 
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM `$tbl_name` WHERE UN='$myusername' and PWD=md5('$mypassword')";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file “adminloginsuccess.php"
session_start();
$_SESSION['user'] = $myusername;
header('location:adminhome.php');
}
else {
header('location:adminloginretry.php');
}
?>

adminhome.php:

<?php $thisPage="Admin Home";
session_start();
if(!(isset($_SESSION['user']) && $_SESSION['user'] != '')) {
header("location:adminlogin.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/meta.php'); ?>
</head>
<body>
<div id="wrapper">
<div id="container">
<div id="header">
<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/header.php'); ?>
<div id="links">
<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/links.php'); ?>
</div><!--close links-->
</div><!--close header-->
<div id="sidebar">
<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/sidebarimage.php'); ?>
</div><!--close sidebar-->
<div id="content">
<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/adminhomecontent.php'); ?>
</div><!--close content-->
<div id="extra" align="center">
<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/fblb.php'); ?>
</div><!--close extra-->
<div id="footer">
<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/footer.php'); ?>
</div><!--close footer-->
</div><!--close container-->
</div><!--close wrapper-->
</body>
</html>

adminhomecontent.php:

You Have Successfully Logged In.<br>
<a href="logout.php">Log Out </a>

现在,由于某种原因,当我去登录时,我被重定向,地址栏上写着www.gemma-hyde-fashion-sketchees.co.cc/adminhome.php,但仍然显示登录表单,如果我查看源代码,我会看到adminlogin.php的源代码。

我是PHP的新手,有人能帮忙吗?我在网上找到了这段代码,所以我试着尽可能全面地理解它

------编辑------我为stackoverflow用户创建了一个登录。如果你转到www.gemma-hyde-fashion-sketchees.co.cc/adminlogin.php并使用用户名stackoverflow和密码stackoverflow,你应该会看到我得到的相同结果(无论如何,此时管理区域实际上没有任何东西)

------柔道编辑------是的,我的意思是,如果我登录,地址栏会显示:http://www.gemma-hyde-fashion-sketches.co.cc/adminhome.php这是我期望重定向到的。但是,如果我右键单击并查看源代码,我会看到

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Gemma Hyde Fashion Sketches | Admin Login</title> 
<meta name="title" content="Gemma Hyde Fashion Sketches | Admin Login" /> 
<meta name="keywords" content="Admin Login,  gemma hyde fashion sketches, fashion, fashion design, fashion sketches, fashion design sketches, clothes design sketches" /> 
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

这与adminlogin.php页面显示的内容相同,这让我想到adminhome.php顶部的这一部分:

session_start();
if(!(isset($_SESSION['user']) && $_SESSION['user'] != '')) {
header("location:adminlogin.php");
}

只是重定向,因为它无法检测到它已登录。

这能解决问题吗?

在我看来,如果你能看到文件的实际PHP(我从"如果我查看源代码,我会看到adminlogin.PHP的源代码"这句话中理解),那么PHP就不会在文件上运行。其他PHP页面能工作吗(即<?php phpinfo();?>)?

如果怀疑会话未设置,请在adminhome.php 中检查

更改此代码

Session_start();
if(!(isset($_SESSION['user']) && $_SESSION['user'] != '')) {
header("location:adminlogin.php");

到这个

Session_start();
 exit(var_dump($_SESSION));

我现在已经解决了这个问题,正是我缺乏经验和聪明导致了这个问题。正如你从下面的评论中看到的那样,这个问题在我的.htaccess文件中。我已经放了RewriteRule ^admin adminlogin.php,所以这是将任何包含admin的页面改回adminlogin.php