Jump to content


Photo

Please respond to easy question


  • Please log in to reply
13 replies to this topic

#1 scs

scs
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts
  • LocationUSA

Posted 03 April 2006 - 05:17 PM

I use dreamweaver so my code is corrent. I'm just not using the right code. I have the session started. When the user logs in I set some session variables.

  $_SESSION['username'] = "sdgjksndgjsg";
  

Now my server uses php4. I'm not sure if that makes a difference. All those session variables are blank when I try to use them. Please respond quickly. I'm in a hurry to finish a project!


#2 phpfreak101

phpfreak101
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 03 April 2006 - 05:43 PM

I think you have to register the session variable first. Like:

session_register('first_name'); 
$_SESSION['first_name'] = $first_name; 


#3 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 03 April 2006 - 05:51 PM

[!--quoteo(post=361277:date=Apr 3 2006, 01:43 PM:name=phpfreak101)--][div class=\'quotetop\']QUOTE(phpfreak101 @ Apr 3 2006, 01:43 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I think you have to register the session variable first. Like:

session_register('first_name'); 
$_SESSION['first_name'] = $first_name; 
[/quote]


just make sure you call session_start(); on all the scripts that are using the session variables and it should work. I had a similar problem when I started using sessions.

#4 phpfreak101

phpfreak101
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 03 April 2006 - 05:54 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
just make sure you call session_start(); on all the scripts that are using the session variables and it should work. I had a similar problem when I started using sessions.[/quote]

Also make sure you put session_start() on your page, before anything else. (HTML etc.) If not, you will get Session errors saying "cannot send header, header already sent" because you sent information to the browser before the session had a chance to start.

#5 scs

scs
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts
  • LocationUSA

Posted 03 April 2006 - 06:02 PM

I used the session register but it didn't work. Now here is the wierd part. I have a if statement that checks for the session variables. And that works fine. So it acts like the variable are there. But when I go to use then below and I get nothing. Like the variable was created. But not set. It did this also before I used the session register.


#6 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 03 April 2006 - 06:11 PM

don't use session_register. use session_start() at the very beginning of your script.

#7 scs

scs
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts
  • LocationUSA

Posted 03 April 2006 - 06:14 PM

Oh, sorry, I guess I was unclear. I start the session at the top of every page. When the user is logging in I use the session register. Then they are redirected to the main page where I say 'Welcome $_SESSION['username']'.

#8 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 03 April 2006 - 06:34 PM

[!--quoteo(post=361291:date=Apr 3 2006, 02:14 PM:name=Zach Attack)--][div class=\'quotetop\']QUOTE(Zach Attack @ Apr 3 2006, 02:14 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Oh, sorry, I guess I was unclear. I start the session at the top of every page. When the user is logging in I use the session register. Then they are redirected to the main page where I say 'Welcome $_SESSION['username']'.
[/quote]

I normally parse them into variables first

$usrname = $_SESSION['username'];
echo<<<endhtml
Welcome back, $usrname
endhtml;

give it a shot.


#9 scs

scs
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts
  • LocationUSA

Posted 03 April 2006 - 06:38 PM

Worked, Thanks!

#10 drewjoh

drewjoh
  • Members
  • PipPip
  • Member
  • 24 posts
  • LocationTexas

Posted 03 April 2006 - 06:46 PM

Also be sure you're not creating what you might think is a local variable with the same name. I just had this problem a few days ago. (If you have register_globals on, which I think is on by default)

If you create a variable called $username and change it, this will also change $_SESSION['username'];

Also, If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister(). ([a href=\"http://us2.php.net/manual/en/function.session-is-registered.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.sess...-registered.php[/a])

Also see: [a href=\"http://us2.php.net/manual/en/security.globals.php\" target=\"_blank\"]http://us2.php.net/manual/en/security.globals.php[/a]


#11 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 03 April 2006 - 06:55 PM

[!--quoteo(post=361301:date=Apr 3 2006, 01:46 PM:name=drewjoh)--][div class=\'quotetop\']QUOTE(drewjoh @ Apr 3 2006, 01:46 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Also be sure you're not creating what you might think is a local variable with the same name. I just had this problem a few days ago. (If you have register_globals on, which I think is on by default)

If you create a variable called $username and change it, this will also change $_SESSION['username'];

Also, If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister(). ([a href=\"http://us2.php.net/manual/en/function.session-is-registered.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.sess...-registered.php[/a])

Also see: [a href=\"http://us2.php.net/manual/en/security.globals.php\" target=\"_blank\"]http://us2.php.net/manual/en/security.globals.php[/a]
[/quote]

Register globals in not default and should DEFINITELY be turned off. It is a security issue.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#12 scs

scs
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts
  • LocationUSA

Posted 03 April 2006 - 07:38 PM

[!--quoteo(post=361301:date=Apr 3 2006, 11:46 AM:name=drewjoh)--][div class=\'quotetop\']QUOTE(drewjoh @ Apr 3 2006, 11:46 AM) View Post[/div][div class=\'quotemain\'][!--quotec--] Also be sure you're not creating what you might think is a local variable with the same name. I just had this problem a few days ago. (If you have register_globals on, which I think is on by default)

If you create a variable called $username and change it, this will also change $_SESSION['username'];

Also, If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister(). ([a href=\"http://us2.php.net/manual/en/function.session-is-registered.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.sess...-registered.php[/a])

Also see: [a href=\"http://us2.php.net/manual/en/security.globals.php\" target=\"_blank\"]http://us2.php.net/manual/en/security.globals.php[/a]
[/quote]
Thanks!
[!--quoteo(post=361304:date=Apr 3 2006, 11:55 AM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Apr 3 2006, 11:55 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]

Register globals in not default and should DEFINITELY be turned off. It is a security issue. [/quote]
Well this current project I'm working on I'm just testing on a free php server. (My testing server on my computer wasn't working) register_globals IS turn on. So that could be a problem. Now my client who will get the project I'm working on, if he uses some one else as a host and register_globals is turn on. Is there a way he could turn that off?

#13 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 03 April 2006 - 07:51 PM

you can change it using .htaccess... but to be honest I don't know how. look it up on google. or maybe someone else knows?

#14 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 04 April 2006 - 04:53 AM

Look at [a href=\"http://www.php.net/manual/en/function.ini-set.php\" target=\"_blank\"]ini_set()[/a] to change your register_globals. The thing is if you use $_GET and $_POST you are compatible with the server whether it uses register globals or not. I strongly urge you to turn it off or request that your server admin does so.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users