范特西_
2017-11-30 14:40
采纳率: 57.1%
浏览 2.2k
已采纳

请教:php中验证用户名和密码正确后,如何实现跳转?

js代码:

 function signin() {
    var name = $('#name').val();
    var password = $('#password').val();
    dataVal = {
        username: name,
        userpassword: password
    };
    $.ajax({
        url: 'verifyOld.php',
        type: 'GET',
        data: dataVal,
        success: function(msg) {
            alert(msg);
        }
    });
}

PHP代码:

 <?php
$username = $_GET['username'];
$userpassword = $_GET['userpassword'];
$link = mysqli_connect("localhost","root","1009976636",'blog');
if(!$link)
{
    $output = 'Unable to connect to the database server.';
    echo $output;
    exit();
}
if(!mysqli_set_charset($link,'utf8'))
{
    $output = 'Unable to set database connection encoding.';
    echo $output;
    exit();
}
$sql = ("select password from user where username = '$username'");
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0) {
    if($userpassword == mysqli_fetch_assoc($result)["password"])
    {
//    $url = 'homepage.html';
//      header("Location: $url");
//      echo "login successuflly";
//      if(isset($url)) 
//      { 
//          header('Location:'.$url); 
//          exit();
//      } 
//      $url = "homepage.html"; 
//      echo "<script language='javascript' type='text/javascript'>"; 
//      echo "window.location.href='$url'"; 
//      echo "</script>"; 
    }
    else
        echo "The username and/or password you specified are not correct.";
}

else {
    echo "The username does not exist!";
}

?>

还望大神不吝赐教。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • Ali 2017-11-30 16:01
    已采纳

    跳转需要在javascript里做

     <?php
    $username = $_GET['username'];
    $userpassword = $_GET['userpassword'];
    
    header('Content-Type: application/json');
    
    $link = mysqli_connect("localhost","root","1009976636",'blog');
    if(!$link)
    {
    
         $output = 'Unable to connect to the database server.';
       echo json_encode(array('status' => 'error','message'=> $output);
        exit();
    }
    if(!mysqli_set_charset($link,'utf8'))
    {
        $output = 'Unable to set database connection encoding.';
            echo json_encode(array('status' => 'error','message'=> $output);
        exit();
    }
    $sql = ("select password from user where username = '$username'");
    $result = mysqli_query($link, $sql);
    if (mysqli_num_rows($result) > 0) {
        if($userpassword == mysqli_fetch_assoc($result)["password"])
        {
         $url = '/homepage.html';
             echo json_encode(array('status' => 'ok','message'=> $url);
        }
        else
            echo json_encode(array('status' => 'error','message'=>  'The username and/or password you specified are not correct.';
    }
    
    else {
        echo json_encode(array('status' => 'error','message'=> 'The username does not exist!');
    }
    
    ?> 
    
    function signin() {
        var name = $('#name').val();
        var password = $('#password').val();
        dataVal = {
            username: name,
            userpassword: password
        };
        $.ajax({
            url: 'verifyOld.php',
            type: 'GET',
            data: dataVal,
            success: function(msg) {
                        if(msg.status == 'ok') {
                              window.location = msg.message
                            }else{
                  alert(msg.message);
                            }
            }
        });
    } 
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • devmiao 2017-11-30 15:56
    评论
    解决 无用
    打赏 举报
  • qq_21416341 2017-12-12 09:01

    成功后后台给个返回码,根据状态来跳转

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题