dongqian9567 2015-10-08 11:39
浏览 39
已采纳

无法使用PHP,MySQL导航到另一个页面

I have some data in the database. As i give the email and password it should fetch the database and login. The problem is, from loginform.php file it is navigating to the loginvalidate.php file, but after that it doesn't navigate to another page which I have mentioned in the header() function. As soon as i give the email and password, and hit the login button, it just stops at loginvalidate.php and doesn't proceed to profile.php file. I hope you get it.I donno where the error is, it is neither throwing an error. I'm using WAMP and when I run this code in the browser, the url should display ../../profile.php after hitting the login button, but it shows ../../loginvalidate.php in the URL and the page has nothing to show i.e blank. I have used the md5 function to store passwords in the database. So here also first I have applied the md5 function on the user entered password and then used the password_verify. Is it the password verifying problem or is it anything else ?

loginform.php

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/jquery.validate.min.js"></script>
</head>
<body>

    <form action="includes/loginvalidate.php" method="post">
        <div style="margin-top:17%;">
         <!--Text box to enter email-id-->
            <input id="email" type="email" name="eid" placeholder="E-mail" required/>
            <hr style="position:relative; color:white;">
        </div>
        <div style="margin-top:3%">
         <!--Text box to enter password-->
            <input id="pwd" type="password" name="password" placeholder="Password" required/>
        </div>  
            <div style="margin-top:15%">
                <input type="image" class="mclass" name="loginbt" id="login"  src="images/login.png">
            </div>
    </form>
</body>

includes/loginvalidate.php

<?php
session_start();
try
{
    $db = new PDO("mysql:host=localhost;dbname=xxx","yyy","zzz");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if(isset($_POST['loginbt']))
    {
        $errmsg='';
        $email =$_POST['eid'];
        $pass = md5($_POST['password']);

        if($errmsg == '')
        {
            $sql = $db->prepare('SELECT id, email, password from users WHERE email = :email');
            $sql->bindParam(':email',$email);
            $sql->execute();
            $result = $sql->fetch(PDO::FETCH_ASSOC);
            if(count($result)>0 && password_verify($pass,$result['password']))
            {
                header('location: ../profile.php');
                exit();
            }
            else
            {
                echo '<script language="javascript">';
                echo 'alert("Username/Password is incorrect")';
                echo '</script>';
            }
        }
    }
}
catch(PDOException $e)
{
    echo $e->getMessage();
}       

?>

  • 写回答

3条回答 默认 最新

  • dongyinzhi4689 2015-10-08 14:32
    关注

    That's all folks, I've found the solution myself. I usually thought that the code I have written was a bit complex. Then after some 99 failed attempts and for 100th attempt, I found the solution myself. This is how loginvalidate.php file should be

    includes/loginvalidate.php

        $db = new PDO("mysql:host=localhost;dbname=xxxx","yyyy","zzzzz");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    
            $email =$_POST['eid'];
            $pass = md5($_POST['password']);            
            $sql = $db->prepare('SELECT id, email, password from users WHERE email = :email AND password = :password');
            $sql->bindParam(':email',$email);
            $sql -> bindParam(':password',$pass);
            $sql->execute();
            $result = $sql->fetchColumn();
            if($result == true)
            {
                header("Location: ../profile.php");
                exit();
            }
            else {
                echo '<script type=text/javascript>';
                echo 'alert("Username/Password is incorrect")';
                echo '</script>';
            }
    

    ob_end_flush();

    I would like to thank each and everyone here who have helped me to find the solution but the main credit finally goes to myself. :P.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器