domsl
domsl

Reputation: 87

PHP: Opening url from database using header

I am pretty new to php (mostly don't do web development at all actually) and, have gotten into it lately. I am trying to make a login page that redirects users after login to a unique URL linked to the user. So I wanted to have say 4 columns in the database: id, username, password, and redirect. And after a successful login to have the URL redirect to the redirect column in the db that is storing the user URL. The problem is the code that I have been working off of. To make a login I cant seem to have it redirect to the db column using the header("location:) line. Here is my code maybe you guys can kindly help me:

<?php
$host="sql1om"; // Host name 
$username="b33_1033"; // Mysql username 
$password="b33_1033"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

So I can't figure out how to add a db column variable to output the url from the redirect column in the

header("location:login_success.php);

Every time I try a variable in that line nothing happens Any help would be appreciated. Please thoroughly explain I am a newbie at php. Thank-you!

Here is edited code that I got working (with help):

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file"login_success.php"
$_SESSION['username'] = $myusername;
$_SESSION['password'] = $mypassword;
$result = mysql_query("SELECT redirect FROM members");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   header("Location: " . $row['redirect']);
}
exit();
}
else {
echo "Wrong Username or Password";
}
?>

Upvotes: 0

Views: 2342

Answers (1)

F21
F21

Reputation: 33401

You need to fetch your results into an associative array:

$row = mysql_fetch_assoc($result)

Then simply use header("Location: $row['redirect']");

  • Enclosing a string in double quotes enable you to insert a variable in there.

  • Alternatively, you can also concatentate: header("Location: " . $row['redirect']);

  • Your SQL results will be stored in an associatively array called $row. To access the redirect column, use $row['redirect']. Of course, $row will only be populated if a row or rows are found.

  • Finally, it is good practice to have an exit() to immediate stop script execution if you are redirecting.

Your code would look like:

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 
$result=mysql_query($sql); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count==1){ 

   // Register $myusername, $mypassword and redirect to file "login_success.php" 
   $_SESSION['username'] = $myusername;
   $_SESSION['password'] = $mypassword;
   $row = mysql_fetch_assoc($result);
   header("Location: $row['redirect']");
   exit();
}else { 
   echo "Wrong Username or Password"; 
} 

Upvotes: 2

Related Questions