dpjs2005 2015-09-16 11:18
浏览 38
已采纳

如何让网站记住登录,以便预先输入输入字段? (PHP,MySQL)

EDIT: Problem fixed. I appreciate the replies I got, both for their help and the rapid response they was. The two answers I got was both rather identical, but I chose one of them to mark it as solved.

I have, for some time tried to get this to work but I can't and thus I am coming here, hoping for help. I am currently trying to create a login, register and comment function on my website. Registering is working, logging in with those credentials also work, and I have got a filter to prevent empty-field entries. Once I log in I have it echo out the possibility to go to the comment page, and my plan there is to allow the users to write a comment, see the comments below, and the author name will be grabbed from their login username. See where I am going? That is exactly what's not working, though. I can't get it to grab the username from the login field, and I am quite sure it has something to do with that the website doesn't remember the login, and I'm unsure how to set cookies and such if thats where the fix would be. So, TL;DR - How do I get the website to remember a login, then insert it into a field?

Above the code is the website design itself, connection with the database, and session_start();. Login.php code

<?php

                if(!isset($_POST['submit_login'])) {
// Checks whether anyone have clicked the submit button, as long as they don't, show the form

                echo '
                    <div class="loginform">
                    <h2>Please login to continue</h2>

                    <br />
                        <form action="login.php" method="POST">
                        Username : &nbsp; <input type="text" name="username_login"><br />
                        Password : &nbsp;&nbsp; <input type="password" name="password_login"><br />
                        <br />
                        <input type="submit" name="submit_login" value="Submit">
                        </form>
                    </div> 
                    ';


                }

                if(isset($_POST['submit_login'])) {
 // Checks whether they have clicked on the submit button or not, if they have, check if the fields are filled or empty, as well as check it with the database.


                    $username_login = $_POST['username_login'];
                    $password_login = $_POST['password_login'];

                    $loginCmd = "SELECT * FROM tblUsers WHERE username='$username_login' AND password='$password_login'";
                    $result = mysql_query($loginCmd);

                    if(empty($username_login)) {

                        echo "<center>Wrong username.</center>";

                    }

                    else if (empty($password_login)) {

                        echo "<center>Wrong password.</center>";
                    }

                    else if (mysql_num_rows($result) == 0) {

                        echo "<center>User does not exist.</center>";

                    }

                    else {

                        mysql_query($loginCmd);
                        echo '<center>Logged in. Welcome '.$username_login.' !</center> <br />';
                        echo '<center>View/post comments<a href="comments.php"> here</center></a>';

                    }

Comments.php code

<div class="commentform">
    <h2>View and post comments and thoughts here!</h2>
        <p>All fields required</p><br /><br />
        <form action="comments.php" method="POST">
            Author : <input readonly type="text" name="author" value=''> <br /><br />
            Comment : <textarea name="comment" class="insertcomment"></textarea><br />
            <br />
            <input type="submit" name="submit" value="Submit">
    </form>
</div> 




<br />
<hr>
<br />

<?php

    if(isset($_POST['submit'])) {

// Checks if they have clicked on the submit button, if they have, send it to the database

        $comment = $_POST['comment'];
        $author = $_POST['author'];

        $insertComment = "INSERT INTO tblComments(comments, author) VALUES ('$comment', '$author')";

        if(empty($comment)) {

            echo "<center>No text found in comment field.</center>";

        }

        else if(mysql_query($insertComment) ) {

            echo "<center>Comment posted</center>";


        }



    }
  • 写回答

2条回答 默认 最新

  • duanchan9354 2015-09-16 11:27
    关注

    Once you validate the login then you can store the username in a session array which is persistent between pages :

    <?php
    // this goes on top of the  php page where ever you want to use session variable
    session_start();
    //once user login is valid then you can store username like this
    $_SESSION["username"]=$username; //$username is what you used while validating the login
    ?>
    

    On some other page you can get this value back as long as you have session_start on top:

    <?php
    //2 nd page 
    session_start();
    echo $_SESSION["username"];//This will print username which you stored while logging
    ?>
    

    For more information on sessions visit w3schools or php.net

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douwendu2460 2015-09-16 11:27
    关注

    You have two options to store the username between pages: Cookies, which are stored on the user's computer, or sessions, which are stored on your server. Consider sessions for any security-based authentication as cookies can be easily manipulated by your users.

    Cookies:

    $expiry_time = time() + 604800; // Expire in 7 days (time is in seconds)
    setcookie("username", "administrator", $expiry_time); // set the cookie
    echo $_COOKIE["username"]; // read the cookie
    

    Sessions:

    session_start();
    $_SESSION["username"] = "administrator"; // set the session variable
    echo $_SESSION["username"]; // read the session variable
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Qt 不小心删除了自带的类,该怎么办
  • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
  • ¥15 有没有哪位厉害的人可以用C#可视化呀
  • ¥15 可以帮我看看代码哪里错了吗
  • ¥15 设计一个成绩管理系统
  • ¥15 PCL注册的选点等函数如何取消注册
  • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
  • ¥15 Python爬虫程序
  • ¥15 crypto 这种的应该怎么找flag?
  • ¥15 代码已写好,求帮我指出错误,有偿!