Jump to content
xisto Community
jailbox

Sessions heeeelllllp

Recommended Posts

Hi. Im making a flatfile chatroom. Ive been making it about a month because I dont understand the sessions. When the user enters the room he/she has to enter a nickname. But when the user enters a message the chatroom "forgets" the nickname. Ive been messing around with sessions but I cant get em to work. Session_start(); thingy is in the right place (at the top). Ive looked through about 10 diferent tutorials but they were useless. How do I make it to pass the nickname variable to multiple pages?And please dont tell me to search google or use php.net because ive already used those.

Share this post


Link to post
Share on other sites

to put something in the session

$_SESSION['variable_name'] = "some_value";
make sure you do not do the following
$variable_name = "another value";
in any part of ur page
it will overwrite the value in ur $_SESSION['variable_name']

Share this post


Link to post
Share on other sites

I'd like to use session_register

 

<?php

if (!session_is_registered('count')) {

session_register('count');

$count = 1;

} else {

$count++;

}

?>

 

<p>

Hello visitor, you have seen this page <?php echo $count; ?> times.

</p>

 

<p>

To continue, <a href="nextpage.php?<?php echo strip_tags(SID); ?>">click

here</a>.

</p>

 

to put something in the session

$_SESSION['variable_name'] = "some_value";
make sure you do not do the following

$variable_name = "another value";
in any part of ur page

it will overwrite the value in ur $_SESSION['variable_name']

4054[/snapback]

Share this post


Link to post
Share on other sites

php sessions may be tricky thats why in order to be absolutely sure you have to completely register the session
below is a script that will register the username variable and will be accesssesible throughout the other pages..

<?phpsession_start();session_register('username');$_SESSION['username'] = $username;?>
by doing this then in all other pages below the page that you registered this. the variable username will be available, to test this create another page and do this
<?PHPsession_start();var_dump($_SESSION);?>
YOU WILL see that you have the variable username in the session array.
hope this helps

Share this post


Link to post
Share on other sites

Don't use sessions. Use Cookies.

Use it something like this:

<?phpif (isset($_COOKIE["user"])){?><h3>Welcome back, <span style="color: red;"><?php print $_COOKIE["user"]; ?></span></h3><?php}else {?><form name="form1" method="get">Enter your username: <input name="username" size="15"><br><br><span style="color: blue; cursor: pointer;" onclick="form1.submit()">Submit</span></form><?php}if (isset($_GET["username"])){setcookie("user",$_GET["username"],time()+9999); //set cookie to expire at that time you can experiment and add more ++ if you want?><h3>Username set!</h3><script language="javascript" type="text/javascript">setTimeout("window.location='./';",2000);</script><?php}?>

Tell me if there's an error and I will correct it.

Share this post


Link to post
Share on other sites

Don't use sessions. Use Cookies.

46735[/snapback]


And what if user has disabled cookies in his browser? :D Then it won't work. The vest way is to... well, there is actualy a best way, but... just append sessionid when calling a new scrpt:

 

Header('Location: somepage.php?'.SID);

Then in that file call session_start(); and use $_SESSION array to access session variables... That HAS to work :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.