f1009976636 2017-11-30 14:40 采纳率: 0%
浏览 2286
已采纳

请教: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);
                            }
            }
        });
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿