Jump to content

$_sessions


tourbike

Recommended Posts

So to protect the other web pages in my app I would add the code:

 

<?

session_start();

if($_SESSION['username']){

header("location:login.php");

}

 

?>

 

and add this to all the pages that I want to protect. !???!

 

this is roughly right, as I cannot seem to get it to work on my localhost machine.

 

cheers

Link to comment
https://forums.phpfreaks.com/topic/208430-_sessions/#findComment-1089172
Share on other sites

A) It would be if(!isset($_SESSION['username'])){

 

B) You need an exit; statement after your header() redirect to prevent the remainder of the 'protected' code on the page from being executed. Without the exit statement all a hacker needs to do is ignore the header() redirect that you are sending to the browser and he can access the remainder of the content on the page.

Link to comment
https://forums.phpfreaks.com/topic/208430-_sessions/#findComment-1089178
Share on other sites

Thanks I changed the code above and slotted it into the page I wanted to protect but when i tried to access the page it didnt work (bearing in mind that I am working on my localhost xampp windows version :( )

 

this is the code that i use to create the $_SESSION once the user logs in:

 

if #Mysql query is correct then returns a value

{

// Register $myusername, $mypassword and redirect to file "control.php"

session_start();

$_SESSION['username'];

$_SESSION['pass'];

header("location:control.php");

}

else {

echo "Wrong Username or Password";

}

 

this works for making the correct passwords etc but its the next step that I am struggling with. the following code I use on control.php to block unauthorised access to the file.

 

<?

session_start();

if(!isset($_SESSION['sm_username'])){

header("location:login.php");

}

exit();

?>

 

where am I going wrong???

Link to comment
https://forums.phpfreaks.com/topic/208430-_sessions/#findComment-1089188
Share on other sites

Just putting two variable names in your php code does nothing -

$_SESSION['username'];

$_SESSION['pass'];

 

You need to assign values to variables -

$_SESSION['username'] = $myusername;
$_SESSION['pass'] = $mypassword;

 

The exit; goes after the header() redirect, not after you close the if(){} statement -

<?php
session_start();
if(!isset($_SESSION['username'])){
header("location:login.php");
exit();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/208430-_sessions/#findComment-1089193
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.