我有一个非常简单的登录系统,它在一个有3列的MySql数据库上工作:-Username -Password -LinkToSite
如果登录成功,点击Submit按钮后,相关的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
}
?>
发布于 2018-06-28 07:01:37
这里你不需要任何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
}
?>
发布于 2018-06-28 06:33:12
这将是你必须编写的一些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>"
}
https://stackoverflow.com/questions/51072296
复制相似问题