Reputation: 19
So more details..
I'm creating a site that goes through a series of pages, with each subsequent page setting a different $_SESSION
variable using the information posted from the page before it. Each page after the first sets a different $_SESSION variable, which is used on that page to query the db to know what information to render. Then after another 3 or so pages, all these different $_SESSION variables are used together to query the db to get the record that matches.
My problem is that on each subsequent page, the $_SESSION variable will be set perfectly fine, but when I proceed to the next page, that same $_SESSION variable from before will now be set to null..
The thing really confuses me is that this only happens on the hosted server I'm using. The site works as expected on my localhost (I use wampserver if that matters), and the code is exactly the same on both.
Page 1
This is at the TOP of the page. I put the "unset()"s there to empty the data at the start of the site.
<?php
session_start();
unset($_SESSION['brand']);
unset($_SESSION['model']);
unset($_SESSION['carrier']);
unset($_SESSION['size']);
?>
Post is near the middle of the page
<div id="select">
<a href="models/page2.php?brand=a" onclick="post"><img src="images/a.png" id="a"/></a>
<a href="models/page2.php?brand=b" onclick="post"><img src="images/b.png" id="b"/></a>
</div>
Page 2
This is at the TOP of the page
<?php
session_start();
if(isset($_SESSION['brand']))
{
$brand = $_SESSION['brand'];
}
else
{
$_SESSION['brand'] = $_GET['brand'];
$brand = $_SESSION['brand'];
}
include('../connection/localconnection.php');
$query = "SELECT DISTINCT Model FROM models WHERE brand = '$brand'";
$result = $dbc->query($query);
if(!$result){
echo "DB error. Could not query database\n";
echo 'MySQL error: ' . mysql_error();
exit;
}
?>
This post is towards the middle of the page. I also echo $_SESSION['brand']
to make sure it is still set.
<form action="../options/page3.php" method="post">
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result))
{
echo "<input type=\"submit\" id=\"c\" name=\"model\" value=\"{$row['Model']}\"/>";
}
} else {
echo "Query didn't return any result";
}
echo $_SESSION['brand'];
?>
</form>
This works perfectly fine on the server and I'll see $_SESSION['brand'] set exactly as its supposed to be.
However, at the bottom of page3.php
I again echo $_SESSION['brand']
and it's set to null so I know the $_SESSION
variable is still in existence, its just empty right?
This scenario is happening on each page and I don't understand why. I'm not the greatest with php but I understand a decent amount and am completely stumped as to why this would only happen on my hosted server and not my localhost. Any help would be greatly appreciated.
Upvotes: 2
Views: 73
Reputation: 945
The session problems for your page might occur because the URL you are opening in the browser are not unique. For example, If say you are creating a login page for your website, and you have created sessions successfully. Now, if you are logging in from URL say http://example.com then your session is limited to this URL only. If you again open the above URL like http://www.example.com (note www. in both urls), then you will see that you are not logged in. So please be sure that your webpage is opening always in single type of URL. Though both URL's will redirect to the same destination, use URL either with www. or without www. Also make sure to include
<?php
session_start();
?>
as the first statement in every php page. For more check this post
Upvotes: 0
Reputation: 67
Just an advice. According to MVC pattern, the session should be handled in Controller. You handle it in View and your View contains too much PHP code.
Upvotes: 1