-1

我创建了一个从我的主页调用的用户登录函数(我们称之为 PHP2)(我们称之为 PHP1)。当用户提交登录信息时,会调用 PHP2 来检查用户名和密码,并在成功或失败时调用 PHP1 页面,其中应显示一条消息欢迎用户或告诉他他的凭据不正确。

因此,PHP1 应该包含“Welcome” + $username 的消息,其中启动时的用户名是空白的,当 PHP2 认证后调用页面时,$username 应该包含用户的登录名。

我该怎么做?

这是我在 PHP1.php 中的表单:

<p>Welcome <?php echo $_POST['user'] ?></p>
<form name="form1" method="post" action="PHP2.php">
   ...
</form>

这是我的 PHP2.php

<?php

   // Connection code

   // username and password sent from form 
   $user = $_POST['user']; 
   $pwd = $_POST['pwd']; 

   // Authentication code

   // Register $user, $pwd and redirect to file "PHP1.php"
   session_register("user");
   session_register("pwd"); 
   session_start();
   header("location:PHP1.php");
?>
4

1 回答 1

0

和 Alma Do Mundo 一样,我建议阅读有关会话的信息。以下是你将如何完成你想要的:

登录.php:

<!DOCTYPE html>
<html>
    <head><title>Login</title></head>
    <body>      
        <form id='loginform' action='checklogin.php' method='post'>             
            <div>Username: <input type='text' id='username' name='username' /></div>
            <div>Password: <input type='password' id='password' name='password' /></div>
            <div><input type='submit' id='submit' value='Login'></div>              
        </form>
    </body>
</html>

让登录页面将数据发送到中间页面,例如 checklogin.php。这是代码:

<?php
    session_start();    

    // check if the username and password are correct
    // in real world scenario, this would be more complex as data
    // is cleaned and then checked against the database where username 
    // and encrypted password (and salt) are stored in a *secured* manner
    if ($_POST['username'] === 'hello' && $_POST['password'] === 'world')
    {
        $_SESSION['username'] = $_POST['username'];
        header('location:welcome.php');
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Login issue</title>
    </head>
    <body>
        <div class='badassredcolor'>Invalid username or password. Please <a href='login.php'>login again</a>.</div>
    </body>
</html>

checklogin.php 将查看用户名和密码是否正确。如果他们是正确的,请记住他们的用户名并将此人发送到 welcome.php。否则请他们重新登录。当然,这个过程应该比这更优雅,但这只是一个例子。

然后,您的欢迎页面将向他们显示欢迎消息,如下所示:

<?php
    session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Welcome to the awesome website</title>
    </head>
    <body>

        <h1>Welcome <?php echo $_SESSION['username']; ?></h1>

    </body>
</html>

希望这可以帮助。

好吧,假设我们想使用单个页面来显示表单并检查登录名/密码,使用下面的示例。这是一个可以修改的快速草稿。在现实世界中,你会做很多不同的事情,但这个例子很适合学习:

<?php
    session_start();

    // let's hold a variable to check if the user POST'ed the form
    // or if we got to this page without POST'ing to itself
    $isPosted = false;

    // let's first check if user POST'ed anything
    if (isset($_POST['username']) && isset($_POST['password']))
    {
        // okay, we got something. Let's flag the variable
        $isPosted = true;

        // Let's check if the username and password are good. If good, let's set up
        // a session variable appropriately
        if ($_POST['username'] === 'hello' && $_POST['password'] === 'world')
        {
            $_SESSION['username'] = $_POST['username'];
        }
    }
    else
    {
        // seems like the form was not posted with any interesting data
        // or it is a fresh page-load. Let's ensure that no session variables
        // are alive
        if(isset($_SESSION['username']))
        {
            session_unset();
        }
    }       
?>
<!DOCTYPE html>
<html>
    <head><title>Login</title></head>
    <body>      

        <?php
        // if the form was posted and session was set, show welcome message
        if (
            $isPosted === true &&
            (isset($_SESSION['username']))
        )
        {
        ?>
            <div class='greetings'>Welcome, <?php echo htmlentities($_SESSION['username']); ?></div>
            <div>more cool things here</div>
        <?php
        }
        // oh, either the page was freshly loaded or session was not set. Cool, show form first
        else
        {
        ?>
            <form id='loginform' action='newlogin.php' method='post'>               
                <div>Username: <input type='text' id='username' name='username' /></div>
                <div>Password: <input type='password' id='password' name='password' /></div>
                <div><input type='submit' id='submit' value='Login'></div>
            </form>

            <?php
            // if the form was posted and we know that session is not set,
            // let's show an error
            if ($isPosted)
            {
            ?>
                    <div class='badassREDcolor'>Invalid username or password</div>
            <?php
            }
        }
        ?>

    </body>
</html>
于 2013-09-10T18:16:31.063 回答