SESSION variables, how do I know they have been set?

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
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

SESSION variables, how do I know they have been set?

Post by jonas »

Code: Select all

if (($active == 1) && ($powerlevel == 4)){
			$_SESSIONї'user'] = $_POSTї'user'];
			$_SESSIONї'powerlevel'] = $powerlevel;
			$_SESSIONї'active'] = $active;
			print("<br><div align="center">You have been authorized to work in the admin area, redirecting in 5 seconds...
				<script type="text/javascript" src="http://www.bolt3.com/redirect.js"></script>
				<script type="text/javascript">
  					var redirect = new Redirect("http://admin.bolt3.com/admin.php");
  					redirect.delay(5);
  					redirect.write();
  					redirect.go();
				</script>
				<br>
			        <a href="http://admin.bolt3.com/admin.php"> Click here to go to the Admin section immediately</a></div><br>");

		&#125; else &#123;
			print("You are not authorized to view this area.");
		&#125;

This is my code for a login I'm trying to make (an Admin login) and this page is fine. It works and I can login if I have the right power level.

Thing is, I set the SESSION variables in this code as you can see and when it redirects to the admin page.. I want to make sure the person just didnt type in the admin url so I recheck to see if SESSION powerlevel is 4, if it isnt they dont get access.

It doesn't seem to be working though. I tried to go $user = $_SESSION['user']; to see if it's setting and I put $user on a page but it displays nothing.

So what am I doing wrong? I remember something about a startsession.
Basically when they login I want it to remember these 3 variables on all pages but not using cookies using sessions.

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

stupid question: session_start() is in the admin.php page?
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

make sure they are getting set and if what they are set with isnt just blank space

Code: Select all

if(!isset($_SESSION['user']))
{
echo 'SESSION NOT SET...baby jesus weeps';
}
else
{
echo 'SESSION SET the value is ' . $_SESSION['user']:
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

or just:

Code: Select all

print_r($_SESSION);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

feyd, nope it isnt. how do i do that. ive been self teaching myself session variables from topics on these boards.

any help ? thanks (show examples would be best!)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

add session_start() just after the beginning of the file (inside php tags) similar to this:

Code: Select all

<?php
session_start();

// other code
?>
this needs to be in each and every page that you want to access session variables in..
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

nice, ok i got it working with your help!

One question, session variables only stay until the browser is closed, correct?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's how they normally work, yes.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's how they normally work, yes.
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

Ok I ran into another problem.

Code: Select all

<?
include("../dbcon/connect_db.inc.php");
session_start();
print_r($_SESSION);
if ($_SESSION&#1111;'powerlevel'] = 4)&#123; 
print("<HTML>
<HEAD>
<TITLE>Admin Options</TITLE>
</HEAD>
<link href="http://admin.bolt3.com/admincss.php" rel="stylesheet" type="text/css">
<table bgcolor="#86846B" width="100%" height="100%" class="regpart2">
<tr>
<td valign="top">
<div align="center">
<a href="main.php" target="main">MAIN</a><BR/>
<a href="addagame.php" target="main">ADD A GAME</a><BR/>
</div>
</td>
</tr>
</table>
</HTML>
");
&#125; else &#123;
print("<HTML>
<HEAD>
<TITLE>No access</TITLE>
</HEAD>
<link href="http://admin.bolt3.com/admincss.php" rel="stylesheet" type="text/css">
</HTML>
");
&#125;
?>
Basically only people with powerlevel of 4 are allowed in the admin section but this isnt working. If you go to http://admin.bolt3.com/admin.php (the redirect url) it still displays... shouldn't it display the else since powerlevel is not 4?

In this example the navigation bar (left frame) should just be blank if its not powerlevel = 4
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if ($_SESSION['powerlevel'] = 4){

you're setting $_SESSION['powerlevel'] to 4..
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

lollerskates.

thanks haha
Post Reply