PHP-登录并打开一个链接

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (71)

我有一个非常简单的登录系统,可以使用3列来处理MySql数据库:-Username -Password -LinkToSite

如果登录成功,则在点击“提交”按钮后,应在浏览器中打开相关的LinkToSite(超链接)。我怎样才能做到这一点?

       <?php
$uname=$_POST['uname'];//username
$password=$_POST['password'];//password 
session_start();
    $con=mysqli_connect("localhost","root","","login");//mysqli("localhost","username of database","password of database","database name")
$result=mysqli_query($con,"SELECT * FROM `login_info` WHERE `uname`='$uname' && `password`='$password'");
$count=mysqli_num_rows($result);
if($count==1)
{
    echo "Login success";
    $_SESSION['log']=1;


}
else
{
    echo "please fill proper details";
    header("refresh:2;url=index.php");// it takes 2 sec to go index page
}
?>
提问于
用户回答回答于

这将是你必须写的一些JavaScript。就像是

if($count==1)
{
    echo "Login success";
    $_SESSION['log']=1;
    echo "<body onpageload=\"dothis();\"></body>

    <script>function dothis(){
    window.location.replace(\"http://stackoverflow.com\");
    }</script>"

}
用户回答回答于

你在这里不需要任何JS,如果使用你可以获得没有页面刷新的结果。但是您的查询和代码不正确,因为您没有得到结果。以下代码将为您提供所需的结果。

<?php

$uname=$_POST['uname'];//username
$password=$_POST['password'];//password 
//session_start(); //Don't need it here
$con=mysqli_connect("localhost","root","","login");
$result=mysqli_query($con,"SELECT * FROM login_info WHERE uname='".$uname."'"); //Assuming the username to be unique in your database
$count=mysqli_num_rows($result);
if($count==1){
    $row=mysqli_fetch_assoc($result);
    if ($password == $row['password']){
        echo "Login success";
        $_SESSION['log']=1;
        echo "<script>setTimeout(function(){window.location = '".$row['LinkToSite']."' ;}, 3000) ; </script>" ; //It takes 2 seconds to get re-directed to URL saved in database
    }
}
else{
    echo "please fill proper details";
    echo "<script>setTimeout(function(){window.location = 'index.php' ;}, 2000) ; </script>" ; //It takes 2 seconds to get re-directed to index page
}

?>

扫码关注云+社区

领取腾讯云代金券