Newbie Question-Sessions...can't get the to work

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
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Newbie Question-Sessions...can't get the to work

Post by matthiasone »

I am tring to run sessions on a set of forms and admin pages on my website. I guess I am confused about where you need to start and restart sessions. I start and register a variable on the main page then when I goto the other page and start the sesion again that variable is not available.

Help

Matt
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Since you're not printing any code I'll write the code that is right. Check if you've done this:
session_start(); //In top of every document you will use session
session_register("my_var"); //The name of your variabel.
$my_var = "Hello"; //Sets the variable to Hello
In you're second script you have to remember to use the session_start() since that have to be in every script that are going to use the session you created.

You're second script:
session_start();
echo $my_var
You can also use $HTTP_SESSION_VARS['my_var'] to get the variable.




Remember: It could be your server that is not set right
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

if your php version is 4.1 or newer then use
$_SESSION['var'] = "value";
to set your variables
and $_SESSION['var'] to get the value of your variables. Zeceer's way will work on older versions but since version 4.1 register_globals is default to off so $var will not be the same as $_SESSION['var']
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Which ever method you use you will need to put session_start() at the top of each page in which you set or use a session variable. Have a look at http://www.php.net/manual/sv/ref.session.php as well.

Mac
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

ok thanks for the suggestions,but the variables still aren't passing

the is the code where the session variables is set

Code: Select all

<?
  require_once("spall_fns.php");
  session_start();
  $_SESSION&#1111;'var'] = $user;

  do_pages($area, $page, $view, $id, $eid, $HTTP_POST_VARS, $mode, $cust);
?>
And here is the it variables is asked for

Code: Select all

session_start();
  echo "u= ".$$_SESSION&#1111;'user'];
...
Anymore ideas?

Matt
rev
Forum Commoner
Posts: 52
Joined: Wed Oct 02, 2002 3:58 pm
Location: Atlanta, GA

Post by rev »

matthiasone wrote: And here is the it variables is asked for

Code: Select all

session_start();
  echo "u= ".$$_SESSION&#1111;'user'];
...
Anymore ideas?

Code: Select all

&lt;?php
session_start();
echo "u=" . $_SESSION&#1111;'user'];
?&gt;
Need only the una dolla'rola. 8O
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

that was not the problem..........I removed it with the same affect

Matt
rev
Forum Commoner
Posts: 52
Joined: Wed Oct 02, 2002 3:58 pm
Location: Atlanta, GA

Post by rev »

Which version of PHP are you using?

Code: Select all

&lt;?php
phpinfo();
?&gt;
Also, within the configure script used to configure your install in PHP, do you see the following:

--disable-session

In your php.ini file (if you are using one, phpinfo() will tell you where PHP is looking for one) does PHP have proper access to write to the location specified in 'session.save_path' (defaults to /tmp, windows defaults to c:/temp I believe - but I know very little when it comes Windows).
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

I am running PHP 4.1.1 on FreeBBS the save directory is set to "/tmp" and the sessions are enabled

Matt :lol:
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

well right off the bat i can tell you that you are setting $_SESSION['var'] in the first page and calling $_SESSION['user'] in the next page. try using the same array index, either 'var' or 'user'
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

On your second page you can also quickly see everything (if anything) in the $_SESSION array by doing:

Code: Select all

echo '&lt;pre&gt;';
print_r($_SESSION);
echo '&lt;/pre&gt;';
On a side note, since you are using PHP 4.1 you can use $_POST instead of $HTTP_POST_VARS.

Mac
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

Well, mydimension if I used the same array index that would make things too easy...lol :lol:

anyway.....thanks to all the help I (we) got it working. Thanks

Matt
Post Reply