【问题标题】:this web page has redirect loop in php此网页在 php 中有重定向循环
【发布时间】:2015-04-09 12:12:20
【问题描述】:

我正在为 php 中的登录页面做一个测试示例(出于学习目的,我故意希望代码不受安全问题的保护,例如 SQL 注入或会话劫持)。我想使用cookies。 索引页面如下所示。只需一个用户名和密码:

当我尝试登录时,我得到这个页面:

你能帮我看看是什么问题导致了这个错误吗?

在 index.php 的开头我使用了以下 php 代码:

   <?php
include('login.php'); // Includes Login Script

if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>

然后,在 login.php 中,使用了以下代码:

{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect("localhost", "root", "", "mydbname");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Selecting Database
$db = mysqli_select_db($connection, "mydbname");
// SQL query to fetch information of registerd users and finds user match.
$query = mysqli_query($connection,"select * from users where password='$password' AND userName='$username'");
$rows = mysqli_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($connection); // Closing Connection
}
}
?>

然后,在 profile.php 中,我使用了以下代码:

    <?php
include('session.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
<b id="logout"><a href="logout.php">Log Out</a></b>
</div>
</body>
</html>

编辑 这是session.php的内容

    <?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect("localhost", "root", "", "mydbname");
// Selecting Database
$db = mysqli_select_db($connection, "mydbname");
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysqli_query($connection,"select userName from login where userName='$user_check'");
$row = mysqli_fetch_assoc($ses_sql);
$login_session =$row['userName'];
if(!isset($login_session)){
mysqli_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}
?>

【问题讨论】:

  • 您似乎在任何地方都没有session_start();,如果是这种情况,那么这将始终保持从一个页面到另一个页面的重定向循环
  • session.php的内容是什么?
  • @shiplu.mokadd.im 已更新。

标签: php mysql apache cookies session-cookies


【解决方案1】:

可能会添加

session_start(); 

$_SESSION 值之前

【讨论】:

  • 我在包含的 session.php 中使用了session_start( );
  • 检查两件事,index.php包含session_start();? var_dump($_POST) 是正确的发布数据吗? $rows == 1 是真的吗?
猜你喜欢
  • 2012-09-22
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 2012-06-07
  • 2013-12-22
  • 2013-11-06
相关资源
最近更新 更多