PHP登录问题

时间:2010-11-11 19:49:03

标签: php mysql authentication

所以我想使用php和MySQL为我的页面创建一个登录脚本。我一直在互联网上搜索关于这方面的所有信息的一个很好的教程,但似乎大多数人只是给了一个脚本并说你去了。问题是我需要有关会话和cookie以及其他内容的更多信息。有谁知道这个东西的好地方?

7 个答案:

答案 0 :(得分:2)

每次使用session_start()时都必须明白;函数,您正在服务器上创建一个新会话,或者如果它已经创建它会引用,所以这会通过浏览器识别每个访问者,同时您创建此会话服务器使用变量在标头上设置cookie变量名为PHPSESSID的变量,该变量的哈希值与会话的ID相同。

在PHP中,你有一个名为$ _SESSION的预定义全局变量,这个变量可以用它来存储数据,例如你登录的标志,就像这样。

$ _ SESSION ['login'] = true;

所以你可以使用这个变量来检查用户是否已经被记录,每次你需要向注册用户显示一些东西。

答案 1 :(得分:1)

您对于会话和Cookie有什么看法?

确保使用http://php.net/manual/en/index.php

但如果您提供更多信息,我们可以帮助您指明更好的方向

编辑: 这看起来像一个非常全面的教程,具有您需要的所有功能: http://www.evolt.org/node/60265

答案 2 :(得分:1)

以下是一些可能有用的参考资料:

答案 3 :(得分:1)

我的身份验证PHP authentication with multiple domains and subdomains,不太安全,将在稍后更新。

答案 4 :(得分:1)

老实说,我不知道你想要什么,因为我自己学会了。因为真的很容易。

所有这一切背后的基本原则是验证输入的登录信息是否正确(通过BL和DA检查,如果你知道我在说什么),然后在<中存储一些关于用户的任意信息strong> $ _ SESSION 变量,如$_SESSION['name'] = 'guest',然后在每个页面的开头写一个简单的语句,要求登录检查$ _SESSION数组的内容,如果没有设置,则重定向到另一个页面等等...... 而已! :d 希望我能回答你的意见! ; - ]

修改 这是一个简单的登录页面:

<?php
session_start();    //required if you want to use the $_SESSION array
$username = $_REQUEST['txtUsername'];   //storing the value of the "txtUsername" text box in the form sent by client after the form has been submited
$password = $_REQUEST['txtPassword'];   //same as $username

if (isset($username) && isset($password))
{
    /*
    * this section depends on the implementation of your BL and DA layers.
    * assume that my BL selects a member by it's username and returns an array
    * containing his/her info
    */
    require_once('mybl.php');   //assume that my BL tier is implemented in the "mybl.php" file
    MyBL $bl = new MyBL();
    $queryResult = $bl->selectByUsername($username);

    //authenticating user
    if ($queryResult['username'] == $username && $queryResult['password'] == $password)
    {
        //the user has been authenticated and can proceed to other pages available for members only

        /* 
        * i'm storing the user's username in the session so that I could refer to it in other pages
        * in case I want to update/modify database tables for this specific user.
        */
        $_SESSION['username'] = $username;
        //store anything else you want for the current user:
        $_SESSION['name'] = $queryResult['name'];   //for welcome prompt

        header('Location: welcome.php');    //redirecting the user
    }
    else    //in case of wrong username/password
    {
        $message = "Incorrect username/password";
    }
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LoginPage</title>
</head>

<body>
<form method="post" action="login.php">
<table style="border: 1px dashed">  
    <tr>
        <td>Username:</td>
        <td><input name="txtUsername" type="text" />
    </tr>
    <tr>
        <td>Password:</td>
        <td><input name="txtPassword" type="password" />
    </tr>
    <tr>
        <td colspan="2" align="center"><input name="btnSubmit" type="submit" value="Login" />
    </tr>
</table>
</form>
<span style="color: red;"><?php echo $message; ?> </span>
</body>
</html>

欢迎页面

<?php
session_start();
//checking to see if the user is logged in
if (!isset($_SESSION['username']))
    header('Location: login.php');  //the user is not logged in, he/she is redirected to the login page.

/*
* Basically you might want to put this code at the beginning of every page that requires
* logging in. This way a user that hasn't logged in can't see the contents of the page
* and you don't have to worry about extra checking and conditions that could raise errors
* due to empty "$_SESSION" array.
*/
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login successful</title>
</head>

<body>
Login successfull!<br />
Welcome <?php echo $_SESSION['name']; ?>
</body>
</html>

答案 5 :(得分:1)

了解PDO数据库操作。这不只是用于登录脚本,而是用于PHP。这将有助于您编写安全程序。

答案 6 :(得分:1)

我认为你遇到的问题是安全的登录脚本带来了MySQL和PHP的许多功能,这是很多教程想要进入的。

有一个非常好的教程here,但它的设置是为了展示如何在PHP4和PHP5中构建登录例程,所以请注意编号的标题并确保您只使用适用于的文件你的PHP版本。请务必查看有关加密密码的部分。这些天,你肯定会做得更好。关于本教程的好处是它包含每个步骤的注释,因此您将知道他们正在做什么,并且可以在需要时搜索更多信息。如果您需要有关特定部分的说明,您可以使用脚本的一部分回复此处。