tpl41803 Posted June 12, 2009 Share Posted June 12, 2009 Hey everyone, working on a script which passes the user's username between different pages on my site. first page is a login, user submits their username through a form, php scripts process's it and i want the person's username to be displayed on the top of each page (similar to how GMAIL has your email address posted on the top of the page when you're logged in) I am using <?php session_start(); $_SESSION['user_name'] = $_POST['user_name']; ?> to start the session and <?php echo $_SESSION['user_name']; ?> to post the username in the body of my page It works fine until I try to load a new page, the username gets lost in the process and I can't figure out how to keep it posted. I have built my pages using includes -- so these two pieces of code are in what i have named "head.inc" -- this is the very top of my page, which includes all <head> info, the <body> tag, and a few other things which are displayed on every page (menu, etc...) is this variable not being carried because I'm using includes? tried just about everything I can think of, please help! Quote Link to comment Share on other sites More sharing options...
tpl41803 Posted June 12, 2009 Author Share Posted June 12, 2009 Also just tried removing the <?php session_start(); $_SESSION['user_name'] = $_POST['user_name']; ?> out of the .inc and put it at the top of each page separately, still doesnt work Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 Just because you have a session_start() and $_SESSION variables on one page and you can assign and echo values on that page, does not mean that sessions are working. Add the following two lines of code for debugging purposes immediately after your first opening <?php tag on the page - ini_set("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
tpl41803 Posted June 12, 2009 Author Share Posted June 12, 2009 thanks tried that seems like it's working alright -- not sure what i would see if it wasn't... with your debugging lines i have the same result so not sure what else to do :-/ Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 So, is your include statement working? Does your file being included have php tags in it? You are not really providing enough information about what you are doing in your code and files for anyone to help. And, you should end all your files with .php extensions so that someone cannot browse to them and see the contents. When you use .inc, the files can be browsed to and the php code and data in them can be seen in the browser. Quote Link to comment Share on other sites More sharing options...
tpl41803 Posted June 12, 2009 Author Share Posted June 12, 2009 yes my include statement is working (using require() yes there are php tags in the included file thanks for letting me know about the .inc files, always sort of wondered what the difference between naming a file as .inc or .php was, really... now i know you seem frustrated with me. i'm not a programmer and i've taught myself php by getting help on forms, reading books, etc... i'm not designing anything tremendously complicated (i don't think) i really just want to know how sessions work and i can't find any really good tutorial on it. i wouldn;t have asked if i hadn't looked around first Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 I didn't say you hadn't looked around or tried, I said that the information you are providing in your posts is not telling us what you have or what you are doing. Based on the lack of errors when the two lines in reply #2 were added and on any other actual information from you, I would guess that you have disabled cookies in your browser or you are including/requiring the file using a URL instead of a file system path. If you post your actual code someone can directly help with what it might be doing wrong. Quote Link to comment Share on other sites More sharing options...
tpl41803 Posted June 12, 2009 Author Share Posted June 12, 2009 OH, I misunderstood... Contents of the required head file--"head.php" <?php session_start(); $_SESSION['user_name'] = $_POST['user_name']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>title</title> <link rel="stylesheet" type="text/css" href="style.css" /> <!--[if IE]> <link rel="stylesheet" type="text/css" href="iestyle.css" /> <![endif]--> </head> <body> <div id="nav_right"> <?php echo $_SESSION['user_name']; ?>| <a href="#">settings</a>| <a href="#">help</a>| <a href="index.html">sign out</a> </div> </div> contents of the actual page (which calls this require) -- this is "index.php" <?php require("head.php"); ?> <div id="page"> <div id="content"> <?php require("sub_head.php"); ?> <?php require("actions.php"); ?> <?php require("mail.php"); ?> <?php require("actions.php"); ?> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 OK, so you are requiring head.php on multiple pages and on the first page you have a value but on other pages you don't? That would be because $_POST['user_name'] only has a value on the first page and when you goto a different page $_POST['user_name'] is empty and that causes $_SESSION['user_name'] = $_POST['user_name']; to set the session variable to an empty value. Quote Link to comment Share on other sites More sharing options...
tpl41803 Posted June 12, 2009 Author Share Posted June 12, 2009 duh... thanks so much! okay, so I would need to do something like <?php session_start(); $_POST['user_name'] = $user_name; $user_name = $_SESSION['user_name']; ?> and then on the subsequent pages the $_SESSSION['user_name'] should work correctly? or am i wrong? Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted June 12, 2009 Share Posted June 12, 2009 more like if(isset($_POST['user_name']) && $_POST['user_name']!='') { $_SESSION['user_name']=$_POST['user_name']; } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 Form processing code (anything that uses expected form data) should be a block of code in your application (not necessarily something that is put into a header.php include/require file) that at a minimum tests if the form has actually been submitted. Quote Link to comment Share on other sites More sharing options...
tpl41803 Posted June 12, 2009 Author Share Posted June 12, 2009 great! managed to get it working and as far as i can tell working correctly! i moved the variables into my processing file (sign_in.php) and everything now works as intended i really appreciate your patience with me :-D Quote Link to comment 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.