Forum Moderators: coopster

Message Too Old, No Replies

Register globals off. How to use cookies & sessions?

         

ayushchd

9:38 am on Jan 19, 2010 (gmt 0)

10+ Year Member



Here everybody.

In my login script I had been using $_SESSION n $_COOKIE..but now the register globals on the server's been turned off since its deprecated.

How do I use COOKIES n SESSIONS now?

CyBerAliEn

4:27 pm on Jan 19, 2010 (gmt 0)

10+ Year Member



The PHP variables _SESSION and _COOKIE are global in PHP. But I am not as familiar with _COOKIE (haven't directly used cookies in years), so I will direct my comments mostly toward _SESSION.

You can access session information from within in class/method/function/name space by directly calling it because it is global and available in all spaces.

However, to access your content, you must declare session_start() near the beginning of your code (before you try to access or use sessions). If you do not declare this PHP function, sessions will not be available.

So suppose you stored some data in your session (key of 'name' and value of 'John Smith' and you wanted to update key 'loggedin' with the value of 'true')...

<?php
$name = $_SESSION['name'];
$_SESSION['loggedin'] = true;
?>

The above would not work, because sessions have not been started.

<?php
session_start();
$name = $_SESSION['name'];
$_SESSION['loggedin'] = true;
?>

The above would work, because sessions have been started.

Remember, to work with sessions in PHP you must open/start them.

If you are doing as above and are still having problems, it may indicate deeper issues (errors in code, logic, etc).