1

I want the users to be redirected to specific user pages. I have a table with username password redirect

I can't seem to write the code for redirect so that windows.location redirects to the links in the database for the specific user who logged in.

dataconnect.php

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

Index.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>

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 * 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";
} 
}
?>

2 Answers2

0

Here i editted my post try this............

while ($row = mysql_fetch_assoc($results)) {
$link=$row['redirect'];
} 
header('Location: "$link"');
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Sindhu
  • 473
  • 6
  • 19
  • I want to pull the links from the database. Its stored in the redirect column. I'm trying to setup a multiuser login like how facebook or flickr is. – user2978016 Nov 11 '13 at 06:12
  • This redirects directly as soon as I press the submit button. I want my jquery code to redirect esp. at this section window.location="home.php"; – user2978016 Nov 11 '13 at 07:02
0

In your jQuery you're posting to login_php.php. Did you mean to post to connect.php instead?

Either way, what you'll probably want to do is just return the link you'd like to redirect to instead of 1 that way if the request is empty you'll continue to do what you're doing (throwing and error) and if it's not empty, you'll have a useful value.

Try updating connect.php to this:

<?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'];
} 
}
?>

This assumes that your redirect column is in the users table. If that's the case then it should return the user's redirect value to your jQuery ajax requester. Then you can use it in the else if statement of your Index.php's ajax callback function:

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);
});
}

Aside from your question, I'd like to strongly recommend that you look into SQL injections. As it stands your connect.php script is susceptible to SQL injections. Basically, you need to escape your $u and $p variables. Use something like mysql_real_escape_string to do so.

Brian Poole
  • 701
  • 3
  • 11