Jump to content

[SOLVED] Session info in an included php file


kjtocool

Recommended Posts

On my index page, the header is an include statement like so:

 

<?php include ("http://www.*****.com/header.php"); ?>

 

This header is in the same location as the "php freaks Get Addicted" blue banner.  Inside that header.php file, I have the following:

 

	<tr>
		<td colspan="5" background="http://www.*****.com/CS5/images/top.jpg" height="100">
			<table width="993" height="97" border="0" cellspacing="0" cellpadding="0">
			  <tr>
			  	<td width="786" rowspan="3"></td>
				<td height="20" class="style97" align="right"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
				  	<?php
						if (isset($_SESSION['user_name'])) {
							$user_name = $_SESSION['user_name'];
							echo '<td width="83%"><div align="right"><span class="style97">You are: ' . $user_name . ' | <a href="http://www.*****.com/logout.php">Logout</a></span></div></td>';
						}
						else {
							echo '<td width="83%"><div align="right"><span class="style97"><a href="http://www.*****.com/login.php?tid=' . $article_ID . '">Login</a> | <a href="http://www.*****.com/forum/ucp.php?mode=register">Register</a></span></div></td>';
						}
					?>
                        <td width="17%"> </td>
                      </tr>
                    </table></td>
			  </tr>
			  <tr>
				<td width="207" height="70" valign="bottom">
					<form name="form2" method="post" action="http://www.******.com/search_movies1.php">
					<table cellpadding="0" cellspacing="0" width="100%">
						<tr>
						  <td><input name="textfield" type="text" style="color:#000000" class="style3" value="" /></td>
						  <td><input type="image" src="http://www.******.com/CS5/images/search.gif" name="Image68" /></td>
						</tr>
					</table>
					</form>					
				</td>
			  </tr>
			  <tr>
				<td height="10"></td>
			  </tr>
		  </table>			
		</td>
	</tr>

 

But even if the session is set, the code never recognizes the set session variable.  I am thinking this is likely because this page has no session start(), but I can't include it because this page is called after code is sent to the browser.  Is there any way to make something like this work?

Link to comment
Share on other sites

OK.  I have an index.php page which contains the following:

 

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

... more code

</head>
<body>

... more code

<?php include ("http://www.****.com/header.php"); ?>

... more code

 

Inside the header.php file, is the php code listed above:

 

<?php
if (isset($_SESSION['user_name'])) {
	$user_name = $_SESSION['user_name'];
	echo '<td width="83%"><div align="right"><span class="style97">You are: ' . $user_name . ' | <a href="http://www.*****.com/logout.php">Logout</a></span></div></td>';
}
else {
	echo '<td width="83%"><div align="right"><span class="style97"><a href="http://www.*****.com/login.php?tid=' . $article_ID . '">Login</a> | <a href="http://www.*****.com/forum/ucp.php?mode=register">Register</a></span></div></td>';
}
?>

 

This code always returns the else value, even if the session variable is set. 

 

My question is, how do I fix this so that this included file can work the way I want.

Link to comment
Share on other sites

The session is set properly, the exact same code listed above works in a separate location of the same file, but it isn't an include.

 

http://www.worldofkj.com/articleIndex.php?tid=31165

 

If you go there, you will see what I mean.  Notice how in the header, you see the "Login | Register", then if you scroll down, you see "Login | Register" right above the comment box.  If a user is logged in, it will properly display their name above the comment box, but not in the header.

Link to comment
Share on other sites

The session is set properly, the exact same code listed above works in a separate location of the same file, but it isn't an include.

 

http://www.worldofkj.com/articleIndex.php?tid=31165

 

If you go there, you will see what I mean.  Notice how in the header, you see the "Login | Register", then if you scroll down, you see "Login | Register" right above the comment box.  If a user is logged in, it will properly display their name above the comment box, but not in the header.

 

You could just use cookies/

Link to comment
Share on other sites

I did not, I assumed it would give me an error.

 

I just now tried this:

 

<?php
session_start();
?>
	<tr>
		<td colspan="5" background="http://www.***.com/CS5/images/top.jpg" height="100">
			<table width="993" height="97" border="0" cellspacing="0" cellpadding="0">
			  <tr>
			  	<td width="786" rowspan="3"></td>
				<td height="20" class="style97" align="right"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
				  	<?php
						if (isset($_SESSION['user_name'])) {
							$user_name = $_SESSION['user_name'];
							echo '<td width="83%"><div align="right"><span class="style97">You are: ' . $user_name . ' | <a href="http://www.****.com/logout.php">Logout</a></span></div></td>';
						}
						else {
							echo '<td width="83%"><div align="right"><span class="style97"><a href="http://www.**.com/login.php?tid=' . $article_ID . '">Login</a> | <a href="http://www.*****.com/forum/ucp.php?mode=register">Register</a></span></div></td>';
						}
					?>
                        <td width="17%"> </td>
                      </tr>
                    </table></td>
			  </tr>
			  <tr>
				<td width="207" height="70" valign="bottom">
					<form name="form2" method="post" action="http://www.****.com/search_movies1.php">
					<table cellpadding="0" cellspacing="0" width="100%">
						<tr>
						  <td><input name="textfield" type="text" style="color:#000000" class="style3" value="" /></td>
						  <td><input type="image" src="http://www.****.com/CS5/images/search.gif" name="Image68" /></td>
						</tr>
					</table>
					</form>					
				</td>
			  </tr>
			  <tr>
				<td height="10"></td>
			  </tr>
		  </table>			
		</td>
	</tr>

 

It doesn't give any errors, but doesn't work either.  However, if you directly link to the included file:

 

http://www.worldofkj.com/header.php

 

The code works if you are logged in, it didn't work before when the session statement wasn't in there.  So it's something about being included that's causing the error.

Link to comment
Share on other sites

I did not, I assumed it would give me an error.

 

I just now tried this:

 

<?php
session_start();
?>
	<tr>
		<td colspan="5" background="http://www.***.com/CS5/images/top.jpg" height="100">
			<table width="993" height="97" border="0" cellspacing="0" cellpadding="0">
			  <tr>
			  	<td width="786" rowspan="3"></td>
				<td height="20" class="style97" align="right"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
				  	<?php
						if (isset($_SESSION['user_name'])) {
							$user_name = $_SESSION['user_name'];
							echo '<td width="83%"><div align="right"><span class="style97">You are: ' . $user_name . ' | <a href="http://www.****.com/logout.php">Logout</a></span></div></td>';
						}
						else {
							echo '<td width="83%"><div align="right"><span class="style97"><a href="http://www.**.com/login.php?tid=' . $article_ID . '">Login</a> | <a href="http://www.*****.com/forum/ucp.php?mode=register">Register</a></span></div></td>';
						}
					?>
                        <td width="17%"> </td>
                      </tr>
                    </table></td>
			  </tr>
			  <tr>
				<td width="207" height="70" valign="bottom">
					<form name="form2" method="post" action="http://www.****.com/search_movies1.php">
					<table cellpadding="0" cellspacing="0" width="100%">
						<tr>
						  <td><input name="textfield" type="text" style="color:#000000" class="style3" value="" /></td>
						  <td><input type="image" src="http://www.****.com/CS5/images/search.gif" name="Image68" /></td>
						</tr>
					</table>
					</form>					
				</td>
			  </tr>
			  <tr>
				<td height="10"></td>
			  </tr>
		  </table>			
		</td>
	</tr>

 

It doesn't give any errors, but doesn't work either.  However, if you directly link to the included file:

 

http://www.worldofkj.com/header.php

 

The code works if you are logged in, it didn't work before when the session statement wasn't in there.  So it's something about being included that's causing the error.

 

 

 

 

You have to include the file after starting the session, make sure you done that.

Link to comment
Share on other sites

Yeah, I know.  Like I posted above, the file in question goes like so:

 

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

... more code

</head>
<body>

... more code

<?php include ("http://www.****.com/header.php"); ?>

... more code

 

So as you can see, the include statement is after the session statement.

Link to comment
Share on other sites

When you include a file using a http:// request, that file is requested through the web server instead of directly though the file system. This causes the file to be parsed and you only include any HTML output that is sent. A session that is started in such a file only exists in that file.

 

You need to use an absolute file system path in your include. See my post in this thread on how to properly include files no matter where they are relative to the current script - http://www.phpfreaks.com/forums/index.php/topic,172711.msg766014.html#msg766014

Link to comment
Share on other sites

So something like this should work?

 

<?php include($_SERVER['DOCUMENT_ROOT'] . "/header.php"); ?>

 

Should I still include the session statement in the file?

 

Edit:  It works beautifully upon testing.  Thanks a bunch, I never knew that about it being requested via web server.

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.