avincent Posted November 9, 2009 Share Posted November 9, 2009 I am trying to pass a session variable from one page to another, but every time I use the following code you are directed to the right page, but it will not display the content of that page. It seems as if the variable is never making it to that next page. I also put in a simple echo command to let me know if the statement on the next page is true, but I got what I asked for the words "Nope". Any suggestions. ---------------------- index.php: <?php session_start(); include "db_connect.php"; if(!isset($_POST['submit'])) { ?> //Display the page here <?php }else{ $user = protect($_POST['user_name']); $pass = protect($_POST['password']); if($user && $pass) { $pass =($pass); //compare the encrypted password $sql="SELECT user_name,company_name FROM `user` WHERE `user_name`='$user' AND `password`='$pass'"; $query=mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($query) > 0) { $row = mysql_fetch_assoc($query); $_SESSION['user_name'] = $row['user_name']; $_SESSION['company_name'] = $row['company_name']; echo "<script type=\"text/javascript\">window.location=\"client-resources.php\"</script>"; } else { echo "<script type=\"text/javascript\"> alert(\"Username and password combination is incorrect!\"); window.location=\"index.php\"</script>"; } } else { echo "<script type=\"text/javascript\"> alert(\"You need to gimme a username AND password!\"); window.location=\"index.php\"</script>"; }}?> ----------------------------------------------------------------------------- redirected page code: <?php session_start(); ?> <div id="main-content"> <?php if(isset($_SESSION['user_name'])) { $user_name = $_SESSION['user_name']; include ("inc/client-resources/client-resources.php"); }else{ echo "Nope"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/ Share on other sites More sharing options...
mrMarcus Posted November 9, 2009 Share Posted November 9, 2009 are you sure you're getting a user_name form the db? try echo'ing out: echo $row['user_name']; for starters; Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954135 Share on other sites More sharing options...
avincent Posted November 9, 2009 Author Share Posted November 9, 2009 I tried your suggestion and commented out the window.location line and replaced it with a echo $row['user_name']; This is what it looks like now: }else{ $user = protect($_POST['user_name']); $pass = protect($_POST['password']); if($user && $pass) { $pass =($pass); //compare the encrypted password $sql="SELECT user_name,company_name FROM `user` WHERE `user_name`='$user' AND `password`='$pass'"; $query=mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($query) > 0) { $row = mysql_fetch_assoc($query); $_SESSION['user_name'] = $row['user_name']; $_SESSION['company_name'] = $row['company_name']; echo $row['user_name']; //echo "<script type=\"text/javascript\">window.location=\"client-resources.php\"</script>"; } else { echo "<script type=\"text/javascript\"> alert(\"Username and password combination is incorrect!\"); window.location=\"index.php\"</script>"; } } else { echo "<script type=\"text/javascript\"> alert(\"You need to gimme a username AND password!\"); window.location=\"index.php\"</script>"; }} I got the correct name from the database "admin". It seems like the database connection is there, but I cannot get it to pass that variable to the next page. Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954138 Share on other sites More sharing options...
avincent Posted November 9, 2009 Author Share Posted November 9, 2009 And the part that really drives me nuts is that it works just fine locally via my wamp server, but once I upload it stops working. Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954140 Share on other sites More sharing options...
mrMarcus Posted November 9, 2009 Share Posted November 9, 2009 for starters, lose the javascript redirection (window.location), and use the header() function: header ('Location: client-resources.php'); exit (0); where client-resources.php is whatever file/page you wish to redirect to .. you must enter a path to that file, ie: header ('Location: /path/to/file/client-resources.php'); exit (0); if it requires one. ok, try uploading a simple test page to test sessions on your uploaded server: <?php session_start() $_SESSION['stanley_cup'] = 'Leafs Win! Leafs Win!'; echo $_SESSION['stanley_cup']; ?> let's just see that you can write sessions to the server properly. Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954156 Share on other sites More sharing options...
avincent Posted November 9, 2009 Author Share Posted November 9, 2009 I put in your recomened code and it looks like this: }else{ $user = protect($_POST['user_name']); $pass = protect($_POST['password']); if($user && $pass) { $pass =($pass); //compare the encrypted password $sql="SELECT user_name,company_name FROM `user` WHERE `user_name`='$user' AND `password`='$pass'"; $query=mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($query) > 0) { $row = mysql_fetch_assoc($query); $_SESSION['user_name'] = $row['user_name']; $_SESSION['company_name'] = $row['company_name']; header ('Location: client-resources.php'); exit (0); } else { echo "<script type=\"text/javascript\"> alert(\"Username and password combination is incorrect!\"); window.location=\"index.php\"</script>"; } } else { echo "<script type=\"text/javascript\"> alert(\"You need to gimme a username AND password!\"); window.location=\"index.php\"</script>"; }} When I tested the page I get the white screen of death The header() function isn't being executed. The page just refreshes. I also put in your test page code, but with the previous code never executing it never gets to that page. Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954161 Share on other sites More sharing options...
mrMarcus Posted November 9, 2009 Share Posted November 9, 2009 sorry, what i should've said is add the following to a file, name it test.php (or whatever you like), and run it separately just to test your servers abilities to execute sessions: <?php session_start(); $_SESSION['stanley_cup'] = 'Leafs Win! Leafs Win!'; echo $_SESSION['stanley_cup']; ?> just a simple test .. i haven't much time today to really invest today, so these little things will help find out the problem. Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954198 Share on other sites More sharing options...
KevinM1 Posted November 9, 2009 Share Posted November 9, 2009 Have you tried turning error reporting on? Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954206 Share on other sites More sharing options...
avincent Posted November 9, 2009 Author Share Posted November 9, 2009 Your code for a test page was successful. The page did display Leafs Win! Leafs Win! I thought error reporting was turned on, but after reviewing the code it was not. The error at the top of the page before submitting the user name is: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/vhosts/######/httpdocs/#######/index.php:1) in /var/www/vhosts/######/httpdocs/######/index.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954226 Share on other sites More sharing options...
avincent Posted November 9, 2009 Author Share Posted November 9, 2009 After looking up that error in a Google search it seems that because I had my error reporting code above my session_start(); it would not execute the session start. I guess I have to have my session_start(); as the very first line in my code. Does this make sense to you guys? Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954233 Share on other sites More sharing options...
oni-kun Posted November 9, 2009 Share Posted November 9, 2009 Yes you do, but assuming you're using header you should use.. <?php ob_start(); //Fix for header session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954241 Share on other sites More sharing options...
avincent Posted November 9, 2009 Author Share Posted November 9, 2009 Thank you all for your help. This was a big help for sure. Quote Link to comment https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/#findComment-954242 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.