0

我目前正在为我的高级项目编写社交网络的页面(它只会在本地运行),并且我遇到了这些重定向错误,我不知道如何解决。大约有三个页面包含“header('location:...') 代码。我不知道它在不同的编码级别会做什么,所以我把所有的编码都放在了等量的缩进中。

索引.php

<? include("inc/incfiles/header.inc.php"); ?>
<?
$reg = @$_POST['reg'];
//declaring variables to prevent errors
$fn = ""; //First Name
$ln = ""; //Last Name
$un = ""; //Username
$em = ""; //Email
$em2 = ""; //Email 2
$pswd = ""; //Password
$pswd2 = ""; //Password 2
$d = ""; //Sign up Date
$u_check = ""; //Check if username exists
//registration form
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("y-m-d"); // Year - Month - Day

if ($reg) {
if ($em==$em2) {
// Check if user already exists
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
// Count the amount of rows where username = $un
$check = mysql_num_rows($u_check);
if ($check == 0) {
//check all of the fields have been filled in
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
// check that passwords match
if ($pswd==$pswd2) {
// check the maximum length of username/first name/last name does not exceed 25 characters
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "The maximum limit for username/first name/last name is 25 characters!";
}
else
{
// check the length of the password is between 5 and 30 characters long
if (strlen($pswd)>30||strlen($pswd)<5) {
echo "Your password must be between 5 and 30 characters long!";
}
else
{
//encrypt password and password 2 using md5 before sending to database
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','d','0')");
die("<h2>Welcome to Rebel Connect</h2>Login to your account to get started.");
}
}
}
else {
echo "Your passwords don't match!";
}
}
else 
{
echo "Please fill in all fields";
}
}
else
{
echo "Username already taken.";
}
}
else {
echo "Your e-mails don't match!";
}
}
?>
<?
//Login Script
if (isset($_POST["user_login"]) && isset($_POST["user_password"])) {
    $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_login"]); // filter everything but numbers and letters
    $password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password_login"]); // filter everything but numbers and letters
    $sql = mysql_query("SELECT id FROM users WHERE username='$user_login' AND password='$password_login' LIMIT 1"); // query the person
    //Check for their existance
    $userCount = mysql_num_rows($sql); //Count the number of rows returned
    if ($userCount == 1) {
        while($row = mysql_fetch_array($sql)){
            $id = $row["id"];
    }
         $_SESSION["id"] = $id;
         $_SESSION["user_login"] = $user_login;
         $_Session["password_login"] = $password_login;
         header("location: index.php");
         exit();
         } else {
         echo 'That information is incorrect, try again';
         exit();
    }
}
?>
<table class="homepageTable">
        <tr>
            <td width="60%" valign="top">
             <h2>Already a member? Login below.</h2>
             <form>
                <input type="text" size="25" name="user_login" id="user_login" placeholder="username" />
                <input type="password" size="25" name="user_password" id="user_password" placeholder="password" /><br />
                <input type="submit" name="button" id="button" value="Login to your account!">
             </form>
            </td>
            <td width="40%" valign="top">
             <h2>Sign up below...</h2>
            <form action="#" method="post">
            <input type="text" size="25" name="fname" placeholder="First Name" value="<? echo $fn; ?>">
            <input type="text" size="25" name="lname" placeholder="Last Name" value="<? echo $ln; ?>">
            <input type="text" size="25" name="username" placeholder="Username" value="<? echo $un; ?>">
            <input type="text" size="25" name="email" placeholder="Email" value="<? echo $em; ?>">
            <input type="text" size="25" name="email2" placeholder="Re-enter Email" value="<? echo $em2; ?>">
            <input type="password" size="25" name="password" placeholder="Password" value="<? echo $pswd; ?>">
            <input type="password" size="25" name="password2" placeholder="Re-enter Password" value="<? echo $pswd2; ?>"><br />
            <input type="submit" name="reg" value="Sign Up!">
            </form>
            </td>
        </tr>
</table>
</body>
</html>

header.inc.php

<?
include ("inc/scripts/mysql_connect.inc.php");
// starts the session
session_start();
// checks whether the user is logged in or not
$user = $_SESSION["user_login"];
if (!isset($_SESSION["user_login"])) {
    header("location: index.php");
    exit();
}
else
{
header("location: home.php");
exit();
}
?>
<html>
<head>
<link href="css/main.css" rel="stylesheet" type="text/css">
<title>Rebel Reach - PHS Student Social Network</title>
</head>
<body>
<div class="headerMenu">
      <div id="wrapper">
            <div class="logo">
                  <img src="img/find_friends_logo.png">
            </div>
            <div class="search_box">
                  <form method="get" action="search.php" id="search">
                  <input name="q" type="text" size="60" placeholder="Search..." />
                  </form>
            </div>
            <div id="menu">
                  <a href="#">Home</a>
                  <a href="#">About</a>
                  <a href="#">Sign Up</a>
                  <a href="#">Login</a>
            </div>
      </div>
</div>
<br />
<br />
<br />
<br />

主页.php

<?
session_start();
$user = $_SESSION["user_login"];
//If the user is not logged in
if (!isset($_SESSION["user_login"])) {
    header('location: index.php');
    exit();
}
else
{
//If the user is logged in
echo "Hi, $user, You're logged in<br />Welcome to what is soon to be your NEWSFEED";
}
?>
4

1 回答 1

3

您的代码中有一个 catch-22:

index.php 包含您的函数库
,该函数库会检查该会话变量是否存在。
如果变量不存在,重定向到 index.php

例如,您编写了一个非常复杂的经典 BASIC 版本10 GOTO 10

于 2013-02-14T18:51:52.000 回答