Jump to content

[SOLVED] $_SESSION not working with included pages


tpl41803

Recommended Posts

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!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.