Link to home
Start Free TrialLog in
Avatar of mychel_normandeau
mychel_normandeauFlag for Canada

asked on

PHP Sessions variables without ANY cookie

Hello!

Is that possible to use session variables under PHP 5 without any cookies (the client browser have cookies OFF).

The session variables must be passed form a page to another (on the same website of course). I want to use them to know in wich language to display the page.

So far I only managed this by passing the SID in each URL on the page but I find it ugly... Is there another way?

Thanks!
Avatar of Matt Kendall
Matt Kendall
Flag of United States of America image

Yeah, you can use session variables without cookies or passing anything.  If you just use a session_start() on each page it'll use the session that was already created, you don't need to pass the SID or anything.  
Avatar of VernieVern
VernieVern

use this on the main page.

<?php
//Start the session
session_start();

// store session data
$_SESSION['language']=english;
?>


on the next page u can use:

<?php

//Start the session
session_start();

//retrieve session data
echo "Language=". $_SESSION['language'];
?>

The output will be English.

As long as you put the start session on top of your page you dont have to bypass anything
Avatar of mychel_normandeau

ASKER

Are you sure it can work? The PHP doc seems to tell that it can't be done whitout cookies or SID...

Try it with those 2 files:

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br>';

echo $_SESSION['favcolor'] . '<br>';
echo $_SESSION['animal'] . '<br>';
echo $_SESSION['time'] . '<br>';

?>
i know for sure it works because i use it all the time at my own website..

Here try this http://vernie.nl/test/page1.php    it is ur two pages and i know for sure i disabled all cookies..

It's not working on your URL... I use Firefox and unchecked "Accept cookies from sites" under tools / options / privacy. Try it and it won't work...
Then i think its something local on your site. because i tried it with firefox, IE6, IE7 and netscape. on 2 different computers.... ill do some more research and report back when i found something that might be the problem.

Gr.
That's because Firefox doesn't make it easy to allow session cookies which is a default thing a lot of good browsers do.  I'm not a big ms fan but I like IE so much more than Firefox when I'm developing web apps.
So other browsers allow session cookies when cookies are turned off in browser settings? Seems odd to me...
I think the reason for that is transient cookies can't really be used to track your usage history or store password/usernames which is the main reason people shut off cookies.
Anything new VernieVern ?
what kind of webserver are you using.. caus maybe its an permission problem.. the account wich is writing the session values need ot have an writing permission.
It didn't worked on YOUR server (http://vernie.nl/test/page1.php) with Firefox (current version) with cookies turned OFF (uncheck "Accept cookies from sites" under tools / options / privacy). Test it and you'll see...
ASKER CERTIFIED SOLUTION
Avatar of Matt Kendall
Matt Kendall
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial