代码之家  ›  专栏  ›  技术社区  ›  shinjuo

PHP登录问题

  •  1
  • shinjuo  · 技术社区  · 15 年前

    所以我想用php和MySQL为我的页面创建一个登录脚本。我一直在网上寻找关于这些信息的好教程,但似乎大多数人只是给出一个脚本,然后说这里你去。问题是我需要更多关于会话和cookies之类的信息。有人知道学习这些东西的好地方吗?

    7 回复  |  直到 15 年前
        1
  •  1
  •   Maghoumi    15 年前

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

    这一切背后的基本原则是验证输入的登录信息是否正确(如果您知道我在说什么,请通过您的BL和DA进行检查),然后在 $会话 $_SESSION['name'] = 'guest' 然后在每个需要登录以检查$u会话数组内容的页面的开头编写一个简单语句,如果未设置,则重定向到另一个页面,依此类推。。。
    希望我能回答你要找的!;-]

    编辑: 下面是一个简单的登录页面:

    <?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>
    
        2
  •  2
  •   alejandro    15 年前

    您必须了解,每次使用session_start();函数时,您都会在服务器上创建一个新会话,或者如果已经创建了会话,则它会生成一个引用,因此这会通过浏览器标识每个访问者,并且在您创建此会话的同时,服务器会使用一个名为PHPSESSID this的变量在头上设置cookie变量变量有一个与会话id相同的哈希值。

    在PHP中,有一个预定义的全局变量$_SESSION,这个变量可以用来存储数据,比如登录时的标志,如下所示。

    $_SESSION['login']=真;

    所以您可以使用这个变量来检查用户是否已经登录,每次您需要向注册用户显示刚刚允许的内容时。

        3
  •  1
  •   Bill    15 年前

    一定要用 http://php.net/manual/en/index.php

    但是如果你提供更多的信息我们可以帮助你找到更好的方向

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

        4
  •  1
  •   cdhowie    15 年前
        5
  •  1
  •   Community Mohan Dere    9 年前

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

        6
  •  1
  •   Cfreak    15 年前

    PDO 用于数据库操作。这不仅适用于登录脚本,也适用于一般的PHP。它将在很大程度上帮助您编写安全的程序。

        7
  •  1
  •   linux4me    15 年前

    我认为您遇到的问题是,一个安全的登录脚本将MySQL和PHP的许多特性发挥出来,这比很多教程都想介绍的要多得多。

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