gpt4 book ai didi

php - 此网页有一个重定向循环 - PHP 登录

转载 作者:可可西里 更新时间:2023-10-31 22:53:57 25 4
gpt4 key购买 nike

我正在用 php 尝试一个登录页面示例。我收到错误消息:此网页有重定向循环详情说:错误代码:ERR_TOO_MANY_REDIRECTS

这是我的代码:

index.php

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

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


<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>

登录.php

<?php
session_start();
$error='';

if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{

$username=$_POST['username'];
$password=$_POST['password'];

$connection = mysql_connect("localhost", "root", "");

$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$db = mysql_select_db("rjtest", $connection);

$query = mysql_query("select * from login where myPassword='$password' AND myUserName='$username'", $connection);
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username;
header("location: profile.php");
} else {
$error = "Username or Password is invalid";
}
}
}
?>

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

$connection = mysql_connect("localhost", "root", "");

$db = mysql_select_db("rjtest", $connection);
session_start();

$user_check=$_SESSION['login_user'];

$ses_sql=mysql_query("select myUsername from login where myUsername='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){

header('Location: index.php');
}
?>

logout.php

<?php
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>

我似乎无法弄清楚为什么。我获得此代码的站点现在处于非事件状态,所以这就是我在这里问这个问题的原因。希望你们能帮助我。很抱歉发了这么长的帖子。

最佳答案

评论回答:

我认为正在发生的事情是您的代码出错了,而您没有看到它,导致它与它应该向您显示的错误作斗争。

您有 $login_session =$row['username']; 使用“用户名”作为行,但您没有在查询中选择它 select myUsername from login where我的用户名

所以,我认为如果该行实际上不存在,您需要做

$login_session =$row['myUsername'];

关于php - 此网页有一个重定向循环 - PHP 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28585551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com