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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历