f1009976636 2017-11-30 06:40 采纳率: 0%
浏览 2294
已采纳

请教: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 08: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 python 下载群辉文件
  • ¥50 代码还没怎么运行但是需要代码功能调用数据
  • ¥15 vue请求不到数据,返回状态200,数据为html
  • ¥15 访问url时不会自动调用其 Servlet的doGet()
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部