dongshan7060 2014-04-27 19:46
浏览 49

我的php登录不会从第一次登录主页

The problem seems to be as follows: I open the browser. Point the browser to localhost/myproject/. login page is loaded on the browser. I enter the email address and password and click login. instead of going to the home page the login page reloaded again. I enter the same email address and password again and click login this time it goes to the home page correctly??. and something increase the ambiguity that the problem appears only for the first login. If I logout and login again from the same browser it respond from the first time. my login code:

<?php
                   session_start();
                   if (isset($_SESSION['email']))
                      header("location:" . $config_basedir);


                    if (isset($_POST['submit'])) {
                        $email = trim($_POST['email']);
                        $email = mysql_real_escape_string($email);
                        $password=  trim($_POST['password']);
                        $password = mysql_real_escape_string($password);

                        $query = "select * from users where email= '" . $email .
                                "' and password= '" . md5($password) . "'";
                        $result = mysql_query($query, $con);

                        if ($result != NULL) {
                            if (mysql_num_rows($result) == 1) {
                                while ($row = mysql_fetch_array($result)) {
                                    $_SESSION['email'] = $email;
                                }
                                header("location: " . $config_basedir);
                            }

                            echo "<p style='color:red ;font-size:15px'>****invalid login***</p>";
                        } else
                            echo "<p style='color:red ;font-size:15px'>****invalid login****</p>";
                    }
                    ?>
                 <html>
                    <form method="post" action="<?php echo $_SERVER['SCRIPTNAME']; ?>">
                        <p><input type="text" name="email" value="" placeholder="Email address"></p>
                        <p><input type="password" name="password" value="" placeholder="Password"></p>

                        <p class="submit"><input type="submit" name="submit" value="Login"></p>
                    </form> 
                  </html>

and at the Home page I use:

 <?php
    session_start();
    if (!isset($_SESSION['email']))
        header("location:" . $config_basedir . "login.php");
  ?>
  • 写回答

1条回答 默认 最新

  • dongshan4316 2014-04-27 19:51
    关注

    Try this:

     $email = mysql_real_escape_string($_POST['email']);
     $password = mysql_real_escape_string($_POST['password');
    

    and change

     if (mysql_num_rows($result) == 1) {
                                    while ($row = mysql_fetch_array($result)) {
                                        $_SESSION['email'] = $email;
                                    }
                                    header("location: " . $config_basedir);
                                }
    

    to:

     if (mysql_num_rows($result) == 1) {
           $_SESSION['email'] = $email;
           header("location: " . $config_basedir);
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭