我有一个非常简单的登录系统,该系统针对3列的MySql数据库进行工作: -用户名 -密码 -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
}
?>
答案 0 :(得分:1)
这里不需要任何JS,如果使用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
}
?>
答案 1 :(得分:0)
这将是您必须编写的一些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>"
}