if the session exists

Soldato
Joined
25 Jan 2003
Posts
11,542
Location
Newark, Notts
simple question, what code do i need to stick in a page to check if a session exists. Basically ive got a page i only want visible to someone logged in, so if the session(username) exists then they can view it, else they can sod off.

Just not sure on the exact code
 
assuming you're using php, i use the following to keep people out of pages i don't want them in :)

Code:
function protectedPage($currentUser)
{
	if ($_SESSION['user_type'] !== $currentUser)
	{
		header("Location: /index.php");
		exit();
	}
}
 
yeh its php, just used:

Code:
if (isset($_SESSION['username'])) {
echo "blabla" }
else {
echo "blabla" }

and it worked nicely
 
another question anyway:

http://www.saywhatagain.co.uk/php/week 9/exercise 2/welcome.php

if you login with "dave" as username, and "david" as password, you can access the grade conversion page, which then decides to plonk a "1" in it for some reason. Similarly, if you try to access grade.php without logging in, it'll plonk a "1" at the bottom of the page.

There isn't a 1 anywhere in my code, i dont know if it has anything to do with the require 'file.php' code im using or whatever, but its doing my head in, WHY IS A 1 APPEARING, any ideas? :p
 
Ripper^ said:
another question anyway:

http://www.saywhatagain.co.uk/php/week 9/exercise 2/welcome.php

if you login with "dave" as username, and "david" as password, you can access the grade conversion page, which then decides to plonk a "1" in it for some reason. Similarly, if you try to access grade.php without logging in, it'll plonk a "1" at the bottom of the page.

There isn't a 1 anywhere in my code, i dont know if it has anything to do with the require 'file.php' code im using or whatever, but its doing my head in, WHY IS A 1 APPEARING, any ideas? :p

would be helpful to see the source code for the page as it's unlikely we can work it out from the html on your site.
 
Gman said:
would be helpful to see the source code for the page as it's unlikely we can work it out from the html on your site.

knew that was coming, ok im guessing its a problem either in grade.php, or grade2.php. Grade.php is the main grade page which checks to see if the session exists, and if it does, it calls grade2.php which has the actual page content.

Grade.php
Code:
<?php
session_start(); // starts the session first to avoid any header errors
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Exercise 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<font face="Verdana, Arial, Helvetica, sans-serif">

<?php
if (isset($_SESSION['username'])) { // checking if the user has logged in
print require 'grade2.php';
print "<br /><br /><a href='welcome.php'>Home</a>";
}
else
{
print require 'welcome.php';
}
?>

</font>
</body>
</html>

Grade2.php
Code:
<?php
session_start(); // starts the session first to avoid any header errors
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Exercise 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<font face="Verdana, Arial, Helvetica, sans-serif">
<h1>Grade point scale converter</h1>

<?php 

// variables declared below

$grade = $_POST['grade'];
$selection = $_POST['selection'];

?>

<!-- simple form to allow the user to select either undergraduate, hnd or masters -->

<form name="form1" method="post" action="<?php echo $PHP_SELF;?>">
<table>
<tr><td><input type="text" name="grade"></td>
<td><select name="selection">
 <option value="undergraduate">Undergraduate</option>
 <option value="hnd">HND</option>
 <option value="masters">Masters</option>
</select></td>
<td><input type="submit" name="submit"></td>
</table>
</form>

<?php

// each line below has the code to determine what will happen when a user selects a value with either undergraduate, masters or hnd.  If the first statement isn't true then elseif's are used for each alternate statement

if ($grade >= "70" && $selection == "undergraduate")
{
echo "Grade points: 13-15, First Class Honours";
}
elseif ($grade >= "60" && $grade <= "69" && $selection == "undergraduate")
{
echo "Grade points: 10-12, Upper Second Class Honours";
}
elseif ($grade >= "50" && $grade <= "59" && $selection == "undergraduate")
{
echo "Grade points: 7-9, Lower Second Class Honours";
}
elseif ($grade >= "40" && $grade <= "49" && $selection == "undergraduate")
{
echo "Grade points: 4-6, Third Class Honours";
}
elseif ($grade >= "0" && $grade <= "39" && $selection == "undergraduate")
{
echo "Grade points: 0-3, You have failed";
}
elseif ($grade >= "70" && $selection == "hnd")
{
echo "Grade points: 13-15, Distinction";
}
elseif ($grade >= "53" && $grade <= "69" && $selection == "hnd")
{
echo "Grade points: 8-12, Merit";
}
elseif ($grade >= "40" && $grade <= "52" && $selection == "hnd")
{
echo "Grade points: 4-7, Pass";
}
elseif ($grade >= "0" && $grade <= "39" && $selection == "hnd")
{
echo "Grade points: 1-3: You have failed";
}
elseif  ($grade >= "70" && $selection == "masters")
{
echo "Grade points: 13-15, Distinction";
}
elseif ($grade >= "60" && $grade <= "69" && $selection == "masters")
{
echo "Grade points: 10-12, Pass With merit";
}
elseif ($grade >= "50" && $grade <= "59" && $selection == "masters")
{
echo "Grade points: 7-9, Pass";
}
elseif ($grade >= "40" && $grade <= "49" && $selection == "masters")
{
echo "Grade points: 4-6, Compensatable Failure";
}
elseif ($grade >= "0" && $grade <= "39" && $selection == "masters")
{
echo "Grade points 1-3, You have failed";
}
else
{
}

?>


</font>
</body>
</html>
 
Back
Top Bottom