kjtocool Posted December 19, 2007 Share Posted December 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 I don't understand your problem, just put it on line:1 session_start(); Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 Post the script where you set the $_SESSION['user_name']; also, try to echo $_SESSION['user_name']; see if it prints out anything. Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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/ Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 I suppose, but would really prefer not to. I tried changing the include statement to a require_once statement, no go. :-\ Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 Did you try to put the session_start() in the included file as well? Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 weird. try <?php include ("header.php"); ?> see what happens/ Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 Same result, but I need the link to be direct in any case, since the header goes across every page of my site, some of which are in sub directories. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 19, 2007 Share Posted December 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 Weird, my includes work just fine with just the filename itself. Wonder if it has something to do with a setting in the PHP.INI file. 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.