Need help, Session ID keep on changing..

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
waichun89
Forum Newbie
Posts: 3
Joined: Sat Aug 14, 2010 12:11 am

Need help, Session ID keep on changing..

Post by waichun89 »

Hi guyz,
I'm facing a problem where my session id is keep on changing, which results in I couldn't properly save my login status and user id for a session. (The link for my page is http://epl.fanxt.com/sestest/2.php)

page 2 and 3 are simply for display purpose, while page 1 is to create user id session, page 4 is to unset the session.

the session works fine if you try this on web browser, but, my problem is, when i access this page using LWUIT application, my session id is keep on changing, so my user id will not be kept for more than 1 page (eg: i create the userid session at page 1, and when i go to page 2 or 3, my user id became blank.)

And i also tried to use cookie to store my user id, the result is same, works fine in web browser, but cannot create a single cookie within the application.

does anyone know the solution to this?

in case if you guyz want to see the code:
Page 1:
<?php
if (session_id() == ""){
session_start();
$bbb=1;
}
else {
$bbb=0;
}

$_SESSION['user_id'] = '123';
$hhh = $_SESSION['user_id'];
$qqq = session_id();

echo "<html><head><title></title></head><body>";
echo "<br>ses start = $bbb ";
echo "<br>session id = $qqq ";
echo "<br>session user_id = $hhh ";
echo "<br><a href='4.php'>click to 4</a>";
echo "<br><a href='2.php'>click to 2</a>";
echo "<br><a href='1.php'>click to 1</a>";
echo "<br><a href='3.php'>click to 3</a>";
echo "</body></html>";

?>
Page 2:
<?php
if (session_id() == ""){
session_start();
$bbb=1;
}
else {
$bbb=0;
}

$hhh = $_SESSION['user_id'];
$qqq = session_id();

echo "<html><head><title></title></head><body>";
echo "<br>ses start = $bbb ";
echo "<br>session id = $qqq ";
echo "<br>session user_id = $hhh ";
echo "<br><a href='4.php'>click to 4</a>";
echo "<br><a href='2.php'>click to 2</a>";
echo "<br><a href='1.php'>click to 1</a>";
echo "<br><a href='3.php'>click to 3</a>";
echo "</body></html>";

?>
Page 3:
<?php
if (session_id() == ""){
session_start();
$bbb=1;
}
else {
$bbb=0;
}

$hhh = $_SESSION['user_id'];
$qqq = session_id();

echo "<html><head><title></title></head><body>";
echo "<br>ses start = $bbb ";
echo "<br>session id = $qqq ";
echo "<br>session user_id = $hhh ";
echo "<br><a href='4.php'>click to 4</a>";
echo "<br><a href='2.php'>click to 2</a>";
echo "<br><a href='1.php'>click to 1</a>";
echo "<br><a href='3.php'>click to 3</a>";
echo "</body></html>";

?>
Page 4:
<?php
if (session_id() == ""){
session_start();
$bbb=1;
}
else {
$bbb=0;
}

unset($_SESSION['user_id']);
$hhh = $_SESSION['user_id'];
$qqq = session_id();

echo "<html><head><title></title></head><body>";
echo "<br>ses start = $bbb ";
echo "<br>session id = $qqq ";
echo "<br>session user_id = $hhh ";
echo "<br><a href='4.php'>click to 4</a>";
echo "<br><a href='2.php'>click to 2</a>";
echo "<br><a href='1.php'>click to 1</a>";
echo "<br><a href='3.php'>click to 3</a>";
echo "</body></html>";
?>
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Need help, Session ID keep on changing..

Post by shawngoldw »

Lose the if statement around session_start().

http://php.net/manual/en/function.session-start.php
"session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie."


Shawn
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: Need help, Session ID keep on changing..

Post by bradbury »

before you check if the id exists its a good idea to start the session and then see what's up with that. I would completely remove the logic at the beginning and just start the session first and just go from there.
waichun89
Forum Newbie
Posts: 3
Joined: Sat Aug 14, 2010 12:11 am

Re: Need help, Session ID keep on changing..

Post by waichun89 »

hmm, but that is why i assign a '1' to $bbb and echo it at the page, it shows that every page runs the session start code, no doubt on this.

and after further research on the LWUIT application, it seems like it experienced the same problem on m.friendster.com and m.twitter.com, but works fine in m.facebook.com,

so i guess this is because of the application's own setting, while facebook works because they uses their own php library (fbml), right?
Post Reply