PHP Session Help

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
RhapX
Forum Commoner
Posts: 30
Joined: Mon Dec 05, 2005 5:24 pm
Location: Seattle, Washington

PHP Session Help

Post by RhapX »

Hello,

I am new to the whole PHP coding scene and am coding a gaming website for a team that I own. I am running into some troubles with the $_SESSION command. If you are logged in, you can view your profile and what not. The trouble comes in when you are able to edit other peoples profiles and there are no administration levels set yet. It seems that I need to save the session for a short amound of time ( preferrably until the user logs out ) but I have no idea where to look to find a simple, easy to learn tutorial on how to do so, and just call upon the SESSION name when needed. If someone could write a quick code with an explanation, I would be very appreciative or even to just post a link to an easy to read and understand tutorial.

Thanks,
- Jake
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

When using sessions within PHP , always put:

Code: Select all

<?php
session_start();
at the very top of your page, before any form of output is sent (this includes whitespace.)

session_start() will create a Session ID for you. It will also store the session ID in the users cookie for you.

If the user already has a valid session id in their cookie, it will automatically parse the session data into the $_SESSION superglobal for you.

Example:
page1.php

Code: Select all

<?
session_start();

$_SESSION['myVar'] = 'boo!';

?>
<html>
<body>
<a href="page2.php">Click me!</a>
</body>
</html>
page2.php:

Code: Select all

<?php
session_start();

echo $_SESSION['myVar'];

?>
When the user clicks the link, it should output the value on page2.php :)
RhapX
Forum Commoner
Posts: 30
Joined: Mon Dec 05, 2005 5:24 pm
Location: Seattle, Washington

Thank you

Post by RhapX »

Wow, now that I understand it better, I was able to get it working.

Thanks,
- Jake
Post Reply