Need help with syntax - very new to php I am trying to use a field (redirect) in mysql to redirect a user to a specific page upon login. Each user has their own private page. My code isn't taking me to my page and I've been unable to determine the correct syntax. Any help greatly appreciated. <?php session_start(); include("include/s_session.php"); $sql = "SELECT redirect FROM users WHERE username = '$_SESSION[username]' AND password = 'md5($_SESSION[password])'"; $result = mysql_query($sql) or die(mysql_error()); if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME) { header('Location: $result"); } else { header ("location: s_loginForm.php"); } ?> Thanks in advance.
Thanks for the input. I have changed that line and now get a msg the the requested file is not found on the server ???
Add print_r($result); exit; before : header("Location: $result"); Then check the output in your screen which show the URL to which you need to redirect and also check that whether that URL is valid.
OK ... Now i'm really confused. value is populated with "Resource ID #14" No idea where that comes from??
Try this ?php session_start(); include("include/s_session.php"); $sql = "SELECT redirect FROM users WHERE username = '$_SESSION[username]' AND password = 'md5($_SESSION[password])'"; $result = mysql_query($sql) or die(mysql_error()); if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME) { header("Location: $result"); } else { header ("location: s_loginForm.php"); } ?> PHP:
Thanks. I've made some progress and I now see that the query is returning the value from the table plus the resource info. The result looks like this: /folder/sub-folder/file.php Resource id #14 How do I get rid of the resource portion???? and just have the data from the redirect field?? Here is the code as it is now: <?php session_start(); include("include/s_session.php"); $result = mysql_query("SELECT redirect FROM users WHERE password = '$_SESSION[password]'") or die(mysql_error()); $row = mysql_fetch_array($result); echo $row['redirect']; if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME) { header("Location: $result"); } else { header ("location: s_loginForm.php"); } mysql_free_result($result); ?>
Here is the final solution. This code works!! <?php //Start session session_start(); include("include/s_session.php"); if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME) { // user is logged in } else { header ("location: s_loginForm.php"); } $result = mysql_query("SELECT redirect FROM users WHERE password = '$_SESSION[password]'") or die(mysql_error()); $row = mysql_fetch_assoc($result); header("Location:" .$row['redirect']); mysql_free_result($result); ?>
Nope it should be like this <?php session_start(); include("include/s_session.php"); if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME) { } else { header ("location: s_loginForm.php"); } $result = mysql_query("SELECT redirect FROM users WHERE password = '$_SESSION[password]'") or die(mysql_error()); $row = mysql_fetch_assoc($result); // Major Fix to redirect header("Location:" .$row['redirect']); mysql_free_result($result); ?> PHP:
Thank you for your input. Much appreciated. See my post from 2:06pm today. We came up with the same solution