Click to See Complete Forum and Search --> : Session not working?
noahssite
Jan 26th, 2010, 02:51 PM
Here is a piece of code that is on the top of every page of my website.
<?php
if(strlen($_SESSION['currPage']) > 0){
$_SESSION['lastPage'] = $_SESSION['currPage'];
}
else
{
$_SESSION['lastPage'] = "http://www.mysite.com";
}
$_SESSION['currPage'] = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
echo $_SESSION['lastPage'];
?>
When going from one page to another, the last page should be echoed to the screen. Instead $_SESSION['lastPage'] is remaining "http://www.mysite.com".
I thought it may have been the lack of session_start(); (which I removed because of header conflicts), but when I added it, I still recieved the same result.
I know $_SESSION['currPage'] is being set, as I commented out the 4th last line to test it.
What could possibly be the problem? Or is there something wrong with the logic?
At the end, this code will be used to utlize a 'Back' button. (Instead of history.go(-1); in javascript)
SambaNeko
Jan 26th, 2010, 03:22 PM
Where did you have session_start(); before? After adding it at the top, your code works properly for me.
If you had it set up properly before, then the problem may be elsewhere - what else are you doing on the pages?
noahssite
Jan 26th, 2010, 03:32 PM
This is being included into another page. When I try to add session_start I get a 'headers already sent' error message.
kows
Jan 26th, 2010, 04:34 PM
that means the page that you're including from is sending output to the browser before the new page is included. it doesn't matter whether this output is your entire page, one space, or one letter that you're accidentally outputting -- any output will stop a header from being sent. usually, the file you're including from should be starting the session anyway. so, if your "main" page needs to output something:
main.php:
<?php
session_start();
include('myfile.php');
?>
hello!
/////
myfile.php:
<?php
$_SESSION['var'] = 'value';
?>
noahssite
Jan 26th, 2010, 09:57 PM
It is working on every page but index.php, the homepage.
I couldn't find any output that was being sent before session_start();
kows
Jan 26th, 2010, 11:58 PM
well, you're sending something. if you're still having problems, post your code. no one can help you otherwise.
noahssite
Jan 28th, 2010, 07:26 PM
Here is my code. I edited out the content.
----------------------------------------------------------
The main page:
<?php session_start(); include('includes/header.php'); ?>
<!--Content Starts Here-->
<div style="position:relative;border-style:solid;border-width:2px;border-color:rgb(76,117,59);position:relative;top:5px;padding:0px;margin:0px;background:rgb(103,135,55);wid th:100%;height:85px;">
<div style="position:absolute;bottom:0px;">
</div>
<img src="images/piggy.jpg" style="border-style:solid;border-width:2px;border-color:rgb(60,117,59);width:110;height:127px;position:relative;right:-68%;top:58%;" />
<img src="images/moneyhouse.jpg" style="border-style:solid;border-width:2px;border-color:rgb(60,117,59);width:110;height:127px;position:relative;right:-68%;top:58%;" />
</div>
<br />
<table align="center" width="100%" border="0">
<tr>
<td width="60%" style="text-align:justify;vertical-align:top;">
<br /><br /><br />
<font style="font-weight:bold;font-size:16px;font-family:'Goudy Old Style',serif;color:rgb(76,117,59);">OUR SERVICES</font>
<div class="bodycontent">
<p>Hello this is the content of the page.</p>
</div>
</td>
<td width="40%" style="text-align:center;vertical-align:top;">
<?php include('includes/rightbar.php'); ?>
</td>
</tr>
</table>
<!--Content Ends Here-->
<?php include('includes/footer.php'); ?>
header.php -
<?php
if(strlen($_SESSION['currPage']) > 0){
$_SESSION['lastPage'] = $_SESSION['currPage'];
}
else
{
$_SESSION['lastPage'] = "http://www.mysite.ca/index_1.php";
}
$_SESSION['currPage'] = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
//echo $_SESSION['lastPage'];
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mortgage Genius</title>
<LINK REL=StyleSheet HREF="/scripts/topmenu.css" TYPE="text/css" MEDIA="screen">
</head>
<body style="background:rgb(195,219,184)">
<table width="980px" border="2" align="center" style="background:white;padding:0px;position:relative;top:5px;">
<tr>
<td style="border-style:none;"> </td>
<td style="border-style:none;"><p style="font-family:'Goudy Old Style', 'Goudy Stout','serif;font-size:24px;font-color:rgb(0,0,0);"><a href="index_1.php"><img src="images/logo.jpg" border="0" style="position:relative;top:1px;right:-1%;width:20%;padding:0px;margin:0px;" /></a><img src="images/IPC_logo.jpg" style="position:relative;top:1px;right:-55%;width:25%;border-style:solid;border-width:3px;border-color:rgb(94,146,73);" /></p>
<div id="mainNav">
<ul class="topmenu">
<li><a href="index_1.php" class="topLevel">HOME</a></li>
<li><a href="our_team.php" class="topLevel">WHO WE ARE</a></li>
<li><a href="what_we_do.php" class="topLevel">WHAT WE DO</a></li>
<li><a href="useful_tools.php" class="topLevel">USEFUL TOOLS</a></li>
<li><a href="calculators.php" class="topLevel">CALCULATORS</a></li>
<li><a href="contact_us.php" class="topLevel">CONTACT US</a></li>
</ul>
</div>
</td>
<td style="border-style:none;"> </td>
</tr>
<tr>
<td style="border-style:none;"> </td>
<td style="border-style:none;">
<div style="height:3px;width:100%;background:rgb(94,146,73);margin:0px;padding:0px;border:2px solid rgb(94,146,73)"> </div>
rightbar.php -
<div align="center" style="width:50%;margin:30%;position:relative;top:30px;">
Content goes here... this will be displayed on the right of the page.
</div>
footer.php -
<br /><br />
</td>
<td style="border-style:none;"> </td>
</tr>
<tr>
<td style="border-style:none;"> </td>
<td style="border-style:none;text-align:center;"><hr />© 2009
<br /><a href="http://www.site.ca" target="_blank" style="color:green;">www.site.ca</a>
<a href="#" style="color:green;"><font style="color:green;">Compliance</font></a>
<a href="#" style="color:green;"><font style="color:green;">Privacy</font></a>
</td>
<td style="border-style:none;"> </td>
</tr>
</table>
</body>
</html>
kows
Jan 28th, 2010, 09:45 PM
before starting editing files, I added the following to the index file I made out of your main page:
<div class="bodycontent">
<p><pre><?php print_r($_SESSION); ?></pre></p>
</div>
then, I rebuilt your project and copied and pasted my index 5 times and renamed them to all of your links (and also changed the link in header.php to index rather than index_1). I also made a small change in the beginning of the header.php file. I replaced your call to strlen() with:
if(isset($_SESSION['currPage'])){
$_SESSION['lastPage'] = $_SESSION['currPage'];
}
this is because my development machine has all errors shown, and $_SESSION['currPage'] will not be initially set. this way, you check if it is set before assigning it a value.
then, I walked through every link to all of your pages. they all worked as expected, and I didn't ever get an error about a session not being set because of headers. if your code isn't working, then you could try doing what I did by changing your call to strlen() (though this really shouldn't have anything to do with it). if it doesn't help, then make sure all of the other links you're visiting are also including the header.php file. that file is doing all of the "magic," and so must be included. you also must have a call to session_start() on any pages trying to deal with sessions. if your our_team.php or contact_us.php files do not call session_start(), then none of this will work.
that leads me to my next point! the way you're doing this is kind of weird. I would suggest setting up one main file that handles all of your requests, and then having other pages that are included that contain all of your content. this recent thread (http://www.vbforums.com/showthread.php?t=600403) has plenty of examples posted by myself on good ways of implementing this. this would make maintenance on your part much easier, too. if you like the look of filenames in your URLs rather than query strings (eg. about.php vs index.php?page=about), you can look into Apache's mod_rewrite to mask your URLs (so that /about/ or /about.php redirects to index.php?page=about) -- or if you're using IIS, I've heard it has some URL rewriting module now.
sorry for the long-winded post!
oh, and if you have any questions about implementing a system where one main file handles all of your requests, I'd suggest posting it in this thread rather than that other one! I'd be happy to help.
gmiller
Jan 29th, 2010, 12:26 AM
You have not add session_start at the starting of PHP code, So first of all you have to add it then your code will work.
kows
Jan 29th, 2010, 09:11 AM
uhh, session_start() was at the beginning of his code. you should probably take a closer look next time.
noahssite
Jan 29th, 2010, 02:20 PM
that leads me to my next point! the way you're doing this is kind of weird. I would suggest setting up one main file that handles all of your requests, and then having other pages that are included that contain all of your content. this recent thread (http://www.vbforums.com/showthread.php?t=600403) has plenty of examples posted by myself on good ways of implementing this. this would make maintenance on your part much easier, too. if you like the look of filenames in your URLs rather than query strings (eg. about.php vs index.php?page=about), you can look into Apache's mod_rewrite to mask your URLs (so that /about/ or /about.php redirects to index.php?page=about) -- or if you're using IIS, I've heard it has some URL rewriting module now.
sorry for the long-winded post!
oh, and if you have any questions about implementing a system where one main file handles all of your requests, I'd suggest posting it in this thread rather than that other one! I'd be happy to help.
That is what I was originally going to do but I didn't think it was a good idea, just in case one page had to have a different layout then the rest of the pages.
Anyway Ill look over my code again, do the changes you mentioned and Ill post back with my progress.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.