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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测