Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.