1

这是一项任务,但我在研究方面做了很多工作,但我觉得我已经走到了尽头。我需要创建一个用户可以登录的页面(login.php),一旦他们登录,他们就会被重定向到索引页面。他们单击登录的链接应替换为注销链接。

但是,注意到所有这些后,首先我确实进入了会话部分,我回显了变量并检索了它们,但是当我在记录会话变量后手动单击 index.php 时,它也不会重定向到 index.php是空的。我在这里做错了什么???

所以这是我在 login.php 中的 php 代码

          $found = false;
          //read the read.txt until the end of file
          while(!feof($inputFile) && $found == false)  
          {

            $line = fgets($inputFile);  
            // replace the special charater within the lines by there proper entity code
            $lineArray = preg_split("/\,/", (string)$line);

            if(strcmp($_REQUEST['email'],$lineArray[2])  && strcmp($_REQUEST['pwd'],$lineArray[4]))
            {
                        $found = true;
                        echo "<script>alert(' FOUND!')</script>";
                        session_start();
                        $myuseremail=$_REQUEST['email'];
                        $mypassword= $_REQUEST['pwd'];

                        $_SESSION['login_email']=$myuseremail;
                        $_SESSION['login_pwd']=$mypassword;
                        setcookie("login_email", $_SESSION['login_email'], time()+60*60*24);
                        setcookie("login_pwd", $_SESSION['login_pwd'], time()+60*60*24);
                        header('Location:index.php');
            }
          }
          fclose($inputFile);

然后在我的 index.php 中,我在我的 html 正文之前包含此代码

    <?php

      session_start();
   if(isset($_SESSION['login_email']) && isset($_SESSION['login_pwd']))
   {
    $user_check=true;
    echo $_SESSION['login_email'];
   }
   else
   {
    $user_check=false; 
   }

?>

在 index.php 中,我也为我的链接添加了这段代码

     <li><a href="index.php">Home</a></li>
 <li><a href="register.php">Register</a></li>

 <?php

if ($user_check){
                 print "<li><a href='logout.php'>Logout</a></li>";
 }
 else{
 print "<li><a href='login.php'>Login</a></li>";
 }
 ?>
            <li><a href="#"> Link 4</a></li>
4

3 回答 3

2

实际上,您的脚本与您可能打算做的相反:

strcmp确实比较了两个参数,但它不返回布尔值。

首先要做的就是将该行更改为:

if ($_REQUEST['email'] === $lineArray[2] && $_REQUEST['pwd'] === $lineArray[4]) {
于 2012-08-04T01:39:01.777 回答
2

I found some errors in your code, all coming down to the same point: You cannot send any custom headers after you have began outputting other data.

Where have you done this?

Here:

echo "<script>alert(' FOUND!')</script>";
session_start();//session_start() sends a cookie to the clients machine. 
//How are cookies sent to clients browsers? Through headers.

And here:

setcookie("login_email", $_SESSION['login_email'], time()+60*60*24);
setcookie("login_pwd", $_SESSION['login_pwd'], time()+60*60*24);
header('Location:index.php');

Personally, I think your code is a complete mess. Because I have nothing better to do, I'll re-write it for you, explaining each step as I go along.

Let's begin:

So the first thing you want to work on is your text file, which stores all the user details.

Instead of using plain lines or whatever, we should use JSON to split users details, from user to user.

So here's what the text file will look like with two users in it:

{"navnav":{"username":"navnav","pass":"deb1536f480475f7d593219aa1afd74c"},"user2":{"username":"user2","pass":"deb1536f480475f7d593219aa1afd74c"}}

Notice how I've also used the username as keys too and how I've hashed the password. So we call this file user.txt and store it somewhere safe.

Now, for the login page, we shall simply get the data through the POST method, compare it, set sessions and tell the user to go somewhere else (redirect them).

session_start();//need to start our session first, of course

//check if any login data has been posted our way
if ( isset($_POST['login']) && !empty($_POST['username']) && !empty($_POST['password']) )
{

//assign input data to temp vars
$username = $_POST['username'];
$password = md5($_POST['password']);//notice how I hash the password 

// get the data fro the text file
$userData = file_get_contents('path to your text file');

//decode the json data to an assoc array 
$userData = json_decode( $userData , true );

//check if the user exists
if ( array_key_exists( $username , $userData ) === false )
{

echo 'the username '.$username.' is invalid.';//notify the user
exit();//bye bye

}//end of user does not exist

//so now we know the user name exists (because we've got to this line)
//we shall compare with the password

if ( $userData['$username']['password'] !== $password )
{

echo 'Your password is incorrect';//notify the user
exit();//bye bye


}//end of incorrect password
else
{

//time to set sessions and stuff
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;

//send the redirect header
header('Location: index.php');
exit();

}//end of password is correct


}//end of login data has been sent

That's all your login code, but you need your html form setup correctly for the right things to be posted with the right names. So use this html form:

<form action="login.php" method="post" name="login" target="_self" id="login">
  <p>
    <label for="username">Username</label>
    <input type="text" name="username" id="username" />
  </p>
  <p>
    <label for="password">Password</label>
    <input type="text" name="password" id="password" />
  </p>
</form>

That's your login page completely sorted.

Now for your index.php:

As you did before, check if the user is logged in and throw the status is in a var:

session_start();//resume your session (if there is one) or start a new one

//set default user status
$userStatus = false;

if ( isset($_SESSION['username']) && isset($_SESSION['password']) )
{

$userStatus = true;

}//end of user is logged in

For your HTML login/logout:

<li><a href="index.php">Home</a></li>
<li><a href="register.php">Register</a></li>

<?php
if ($userStatus === true){
echo "<li><a href='logout.php'>Logout</a></li>";
}
else{
echo "<li><a href='login.php'>Login</a></li>";
}
?>
<li><a href="#"> Link 4</a></li>

And there you have it.

Let me know if you have any problems.

One more thing:

This is far from secure. Why? You're using text files, you're using text files and you're using text files.

EDIT:

To separate the JSON data by user, simply edit the text file manually (see my comment).

Or you could just paste this into your text file:

{"navnav":{"username":"navnav","pass":"deb1536f480475f7d593219aa1afd74c"},
"user2":{"username":"user2","pass":"deb1536f480475f7d593219aa1afd74c"}}

Do you see how there is no \n in the above? Because I just created a new line manually (by just hitting enter). \n will make the JSON code invalid, so that's why you should avoid it. This method just means if you have to create new users, and you need a new line for each user, then you will have to do it manually.

于 2012-08-04T03:12:56.960 回答
1

首先,在某些输出发生后永远不要发送标题,即当你这样做时echo "<script>alert(' FOUND!')</script>";被认为是输出,PHP 将生成一个标题,稍后你的标题行将被忽略。

尝试在不先发送任何输出的情况下使用标头,这应该可以解决重定向问题。

至于会话信息被擦除的原因,请在重定向上尝试以下行:

header('Location:index.php?'.SID);

它不应该是必需的,但值得一试。

于 2012-08-04T01:47:54.920 回答