1

我希望将用户重定向到特定的用户页面。我有一个带有用户名密码重定向的表

我似乎无法编写重定向代码,以便 windows.location 重定向到登录特定用户的数据库中的链接。

数据连接.php

 <?PHP
    @mysql_connect("localhost","root","")
    or die("could not connect to mysql");
    @mysql_select_db("login")or die("no database");
    ?>

索引.php

<html> 
<head> 
<title>Animated Login</title>
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="login.css">
<script type="text/javascript">
$(function() {
$(".center").animate({
   opacity: 100.0,
   left: '+=800',
   height: 'toggle'
   }, 5000, function(){
});
$(".sign_b_btn").live("click",function() {
var u=$("#u").val();
var p=$("#p").val();
if(u==""){
$("#u").css("border-color","red");
$("#un").css("color","red");
$(".error").show().html("Please enter your username.");
$("#p").css("border-color","#606060");
$("#up").css("color","#333333");
}else if(p==""){
$("#u").css("border-color","#606060");
$("#un").css("color","#606060");
$(".error").show().html("Please enter your password.");
$("#p").css("border-color","red");
$("#up").css("color","red");
}else{
dataString = 'u=' + u + '&p=' + p;
$.ajax({
type: "POST",
url: "login_php.php",
data: dataString,
cache: false,
success: function(html){
if(html == "" ){
$(".error").show().html("The email or password you entered ois incorrect.");
$("#p,#u").css("border-color","red");
$("#up,#un").css("color","red");
}else if(html == "1"){
$(".error").fadeOut(1000);
$("#u").css("border-color","#606060");
$("#un").css("color","#333333");
$("#p").css("border-color","#606060");
$("#up").css("color","#333333");
$(".center").animate({
   opacity: 0.25,
   left: '+=900',
   height: 'toggle'
   }, 5000, function() {
$(".done").slideDown(200).html("Welcome "+u); 
setTimeout(function(){
var u=$("#u").val("");
var p=$("#p").val(""); 

window.location="home.php";
}, 5000);
});
}
}
});
}
}); 
});
</script>
</head>
<body>
<div class="main">
<div class="done"></div>
<div class="center">
<div class="title">Login</div>
<div class="error"></div>
<div class="input"><div class="left" id="un">Username:</div><div class="right">
<input type="text" class="log" id="u"></div><div class="c"></div></div>
<div class="input"><div class="left" id="up">Password:</div><div class="right">
<input type="password" class="log" id="p"></div><div class="c"></div></div>
<div class="sign_b_btn"><div class="sign_btn">Sign In</div></div>
</div>
</div>
</body>
</html>

连接.php

<?PHP 
include('dataconnect.php');//Your connection to your database

//Get posted values from form
$u=$_POST['u'];
$p=$_POST['p'];

//Strip slashes
$u = stripslashes($u);
$p = stripslashes($p);

//Strip tags 
$u = strip_tags($u);
$p = strip_tags($p);

$p=md5($p);
$check = mysql_query("SELECT * FROM user WHERE user ='$u' 
AND pass='$p'")or die(mysql_error());
$check = mysql_num_rows($check);
if($check !== "0"){
$results = mysql_query("SELECT * FROM user WHERE user ='$u'") or die(mysql_error());
while ($row = mysql_fetch_assoc($results)) {
    $user=$row['user'];
    session_register('user'); 
    $_SESSION['user'] = $user;
echo"1";
} 
}
?>
4

2 回答 2

0

在这里我编辑了我的帖子试试这个............

while ($row = mysql_fetch_assoc($results)) {
$link=$row['redirect'];
} 
header('Location: "$link"');
于 2013-11-11T06:06:24.640 回答
0

在您的 jQuery 中,您发布到login_php.php. 你的意思是发帖connect.php代替吗?

无论哪种方式,您可能想要做的只是返回您想要重定向到的链接而不是 1 如果请求为空,您将继续做您正在做的事情(抛出和错误)和如果它不为空,您将获得有用的价值。

尝试更新connect.php到这个:

<?PHP 
include('dataconnect.php');//Your connection to your database

//Get posted values from form
$u=$_POST['u'];
$p=$_POST['p'];

//Strip slashes
$u = stripslashes($u);
$p = stripslashes($p);

//Strip tags 
$u = strip_tags($u);
$p = strip_tags($p);

$p=md5($p);
$check = mysql_query("SELECT * FROM user WHERE user ='$u' 
AND pass='$p'")or die(mysql_error());
$check = mysql_num_rows($check);
if($check !== "0"){
$results = mysql_query("SELECT user, redirect FROM user WHERE user ='$u'") or die(mysql_error());
while ($row = mysql_fetch_assoc($results)) {
    $user=$row['user'];
    session_register('user'); 
    $_SESSION['user'] = $user;
    echo $row['redirect'];
} 
}
?>

这假定您的重定向列在用户表中。如果是这种情况,那么它应该将用户的重定向值返回给您的 jQuery ajax 请求者。然后你可以在你的 ajax 回调函数的else if语句中使用它:Index.php

else if(html != ''){
var redirect_url = html;
$(".error").fadeOut(1000);
$("#u").css("border-color","#606060");
$("#un").css("color","#333333");
$("#p").css("border-color","#606060");
$("#up").css("color","#333333");
$(".center").animate({
   opacity: 0.25,
   left: '+=900',
   height: 'toggle'
   }, 5000, function() {
$(".done").slideDown(200).html("Welcome "+u); 
setTimeout(function(){
var u=$("#u").val("");
var p=$("#p").val(""); 

window.location=redirect_url;
}, 5000);
});
}

除了您的问题,我强烈建议您研究 SQL 注入。就目前而言,您的connect.php脚本容易受到 SQL 注入的影响。基本上,您需要转义您的$u$p变量。使用类似的东西mysql_real_escape_string

于 2013-11-11T07:16:07.823 回答