Sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Sessions

Post by filch »

I have searched about this forum and tried a number of things that have been suggested and I have read a good portion of the manual with regards to sessions and still can't see what I am missing. Could someone cast a critical eye over the following and spot what is probably a stupid mistake?

I am trying to allow a user to choose which currency to use on an ecommerce site and by choosing, set up a session var with a directory value stored in it that I can access to determine which sub site to use. Here is my code from the entry page that is relavent:

Code: Select all

<?php
session_start();
if (isset($_SESSION&#1111;'path'])) &#123;
	if ($_SESSION&#1111;'path'] == "/canada") &#123;
		header ("Location: /canada/index.php");
	&#125; elseif ($_SESSION&#1111;'path'] == "/us") &#123;
		header ("Location: /us/index.php");
	&#125;
&#125;
?>
On an image link in the page I have for example:

Code: Select all

<a HREF="/canada/index.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('currency_page_r4_c2','','/assets/images/index/currency_page_r4_c2_f3.gif',1)"><img SRC="/assets/images/index/currency_page_r4_c2.gif" ALT="" NAME="currency_page_r4_c2" WIDTH="145" HEIGHT="20" BORDER="0" onClick="<?php $_SESSION&#1111;'path'] = "/canada" ?>"></a>
When the user goes to the next page, for instance "/canada/index.php", I have the following code at the top of the page to help with testing:

Code: Select all

<?php 
session_cache_limiter('none');
session_start();?>
<?php 
if (isset($_SESSION&#1111;'path'])) &#123;
	echo "Session set and Chosen Directory is: " . $_SESSION&#1111;'path'] . "<br>";
&#125; ?>
Now when the user first comes here, the output indicates that $_SESSION['path'] has been set to "/canada".

Now if the user goies to another page in this subsite, the session value seems to be gone or not available. I use the same code as above to test if the value is there and nothing is output. Also, I have a link on all these sub pages to unset $_SESSION['path'] and take the user back to the entry page to choose another currency. The code is:

Code: Select all

<a HREF="/index.php" onClick="XMkillCookie('currency')"  onMouseUp="<?php unset($_SESSION&#1111;'path']) ?>">SWITCH CURRENCY</a>
The session save path is set to /tmp, which exists on the root of my server but not on the doc root of my site, which I assume is correct and the permissions are set correctly.

Anyone have any help for me? I would sincerely appreciate it as this is driving me a bit crazy.

Cheers

Dave
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

In addition

Post by filch »

Just checked my /tmp directory for the session and this is what is written to the session itself:

path|s:7:"/canada";

So I assume from this that path is the name and "/canada" is the value but what is the other stuff? I thought it was only suppose to be a name:value pair?

Dave
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ok... I took a crack at it. Here's what I came up with.

Image Code:

Code: Select all

&lt;form action="handler.php" method="post"&gt;
	&lt;input type="image" src="../mySite Tester/images/splash.png.jpg" name="path" value="/canada"&gt;
&lt;/form&gt;
handler.php:

Code: Select all

<?php

$path = $_POST['path'];
$_SESSION['path'] = $path;

echo 'Your code would be <strong>header("Location:  '.$_SESSION['path'].'/index/page.php);</strong>';

?>
Outputs:

Your code would be header("Location: /canada/index/page.php); which is what you are after, no?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

onclick(<?PHP $_SESSION['path']="/canada"; ?>) won't work anyways.
PHP is run BEFORE its output, you cant run it when clicked.

you can however submit the value of what they clicked
<a href="pick.php?loc=/canada"><img src=""></a>
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

LiLpunkSkateR wrote: you can however submit the value of what they clicked
<a href="pick.php?loc=/canada"><img src=""></a>
Yep... Ya know, I thought about doin this, but it seemed like he wanted to keep it as a POST type submission...
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Sessions

Post by filch »

So, you are saying that you cannot run a piece of php code from an onClick handler and that the only way, or at least one way to do this is thru a form post from the image? I guess what I am not clear about here is that a session cookie appears to have been written to my /tmp directory as I referred to in my previous post, using the code I have here. It does not appear to be properly formatted so perhaps that is why I cannot seem to access it?

I will mess with your suggestions and see what I come up with. Thanks for your replies.

Cheers

Dave
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Post by filch »

Well, I have taken your suggestions and for the most part I think I have it except for one issue. I no longer have the error message so that is good. In the mail body of the page, rather than use php code, I use a form that surrounds each link image and use the following code:

Code: Select all

form ACTION="/nav.php" METHOD="POST" NAME="canada" ID="canada"&gt;&lt;input  TYPE="IMAGE" SRC="/assets/images/index/currency_page_r4_c2.gif" WIDTH="145" HEIGHT="20"&gt;
       &lt;input NAME="path" TYPE="hidden" ID="path" VALUE="/canada"&gt;
   &lt;/form&gt;
and nav.php looks like this:

Code: Select all

<?php 
session_start();
$curPath = $_POST['path'];
$_SESSION['path'] = $curPath;
if (isset($_SESSION['path']) && $_SESSION['path'] == "/canada") {
	header("Location: /canada/" . $_POST['linkpage'] . "");
} else {
	header ("Location: /us/" . $_POST['linkpage'] . "");
}
?>
Now this seems to work and the user is directed to whatever page they chose and as well I out put the value of $_SESSION['path'] on that page and it is correct. However, it is lost as soon as I go to another page. The output from this session is being written to my /tmp directory on my server but it is empty. Checking my php install with phpinfo() says that everything should be fine.

If anyone sees what I am doing wrong, I would appreciate a response.

Cheers

Dave
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

you sure you're using session_start(); on the other pages?
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Session

Post by filch »

Yup .. made sure I checked that. However, perhaps some clarification. I have been led to believe that session_start() had to be called right at the top with no spaces before or after it.

i.e.

<?php
session_cache_limiter('none');
session_start();?>

as opposed to
<?php
blah blah more code here;
session_cache_limiter('none');
session_start();
other code here;
?>

What is the correct way? I assume that the first way is fine as long as you have nothing above the code but it seems you can have more code after it.

Cheers

Dave
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You can put session_start() before any output, output is basically anything sent to the browser/client.

So,
<?php
$foo = 'hello';
foreach(){ .. etc.. }
...more php code here..
session_start();

is ok (as long as the PHP above it causes no actual ouput) , but,
<html>
..more html here..
<?php
session_start()

isn't ok as the html gets sent to the client and is classed as output.
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

session_start

Post by filch »

Ah .. ok ... very similar to headers then. Great, thanks.

Dave
Post Reply