Working with Sessions in PHP

They have: 71 posts

Joined: Aug 2001

Hey guys,

quite a simple question (I think) if you know sessions well.. and I don't.

How do I update a session variable?

Regards,
Adam.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Just like you do any other variable. Just assign a value to it.

Make sure you register your session variables at every page, or they will not transfer over to the next page.

If you have a problem with some code, feel free to post it.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 71 posts

Joined: Aug 2001

I figured it out earlier but thanks anyway Mark.

One more question to you all...

I have a field called "address", when someone types their address into this field it records it to the db with . When I call the address up later on though (in a textarea) it prints the 's, how do I get it so it goes down a line in the textarea?

Thanks again,
Adam.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Two options:

1) Don't record in the DB. Instead, put a \n. Then when your printing the value to the textarea, use nl2br(). (my prefered)

2) use:
$string = preg_replace("##i", "\n", $string);

PHP Docs: nl2br(), preg_replace()

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 71 posts

Joined: Aug 2001

I tried last night and it worked fine but today it's not? weird.

Anyway, someone changes their password in a form (I called the textbox 'pwd') when they click submit,

<?php
session_unregister
(\"pwd\");
session_register(\"pwd\");

$query = \"UPDATE members SET password='$pwd' WHERE username='$uid'\";
mysql_query(
$query);
?>

I know that it works because it doesn't sign you out (as it would if the sessions didn't overwrite) and I know that it submits to the database the problem is what it submits it just submits it as if the field was blank, here is the code for the password textbox

" name="pwd">

Do you know what might be wrong here?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I don't know what is wrong, but try this:

<?php
echo $pwd;
session_unregister(\"pwd\");
echo
$pwd;
session_register(\"pwd\");
echo
$pwd;

$query = \"UPDATE members SET password='$pwd' WHERE username='$uid'\";
mysql_query(
$query);
?>
Does $pwd change?

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.