dongtanlin0765 2014-04-10 17:58
浏览 70

php登录脚本不起作用[重复]

This question already has an answer here:

i have got a problem with my login script. i have a small div, where i'd like to echo a form so users can log in. after submitting the form, the values will be sent to another page which create a session. After that, when the user is back on the index, where the login form was, there should be no login form anymore but the info about the user. But the problem: There's nothing echoed in the div, even if there is no session created yet.

<div id="updates">
    <?php
        if(isset($_SESSION['username']))
        {
            $query = mysql_query("SELECT * FROM users WHERE username='$dbusername'");

            $numrows = mysql_num_rows($query);

            if ($numrows!=0)
            {
                while ($row = mysql_fetch_assoc($query))
                {
                    $dbusername = $row['username'];
                    $dbpassword = $row['password'];
                    $dbvipshouts = $row['vip_shouts'];

                    echo"
                    <table>
                        <tr>
                            <td><font color='white'>Nickname:</font></td><td>$dbusername</td>
                        </tr>
                        <tr>
                            <td><font color='white'>Vip shouts over:</font></td><td>$dbvipshouts</td>
                        </tr>
                    </table>";

                }
            }
            else
                {
                echo "
                <form name='login' action='login.php' method='POST'>
                <table>
                    <tr>
                    <td><font color='white'>Nickname:</font></td><td><input class='inputname' type='text' name='nickname'></td>
                    </tr>
                    <tr>
                    <td><font color='white'>Wachtwoord:</font></td><td><input class='inputname' type='password' name='password'></td>
                    </tr>
                    <tr>
                    <td colspan='2'><center><input class='inputname' type='submit' name='submit' value='Log in!'></center></td>
                    </tr>
                </table>
                </form>";
                }
        }
    ?>
    </div>

and the code of the file that should create a session:

<?php

        $username = $_POST["nickname"];
        $password = $_POST["password"];

        if ($username&&$password)
        {

            $query = mysql_query("SELECT * FROM users WHERE username='$username'");

            $numrows = mysql_num_rows($query);

            if ($numrows!=0)
            {
                while ($row = mysql_fetch_assoc($query))
                {
                    $dbusername = $row['username'];
                    $dbpassword = $row['password'];
                    $dbvipshouts = $row['vip_shouts'];
                }

                // controleren of ze bij elkaar passen
                if ($username==$dbusername&&$password==$dbpassword)
                {
                $_SESSION['username']==$dbusername;
                $_SESSION['vip_shouts']==$dbvipshouts;
                header('Location:index.php');
                }
                else
                    echo "Gebruikersnaam of wachtwoord is onjuist! Klik <a href='index.php'>hier</a> om terug te gaan.";

            }
            else
                die("Deze gebruiker bestaat niet! Klik <a href='index.php'>hier</a> om terug te gaan.");
        }

        else
            die ("Voer een gebruikersnaam en wachtwoord in! Klik <a href='index.php'>hier</a> om terug te gaan.");


    ?>
</div>
  • 写回答

2条回答 默认 最新

  • douqi2571 2014-04-10 18:02
    关注

    you have to use session_start() starting of page,wherever you want to create or use the session data

    example

    <?php
    
    session_start();
    
    $_SESSION['id']=1;
    
    ?>
    

    in other page

    <?php
    
    session_start();
    
    echo $_SESSION['id'];
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题