Jump to content

$_sessions


tourbike

Recommended Posts

hello,

 

I am trying to protect in internal web app pages being accessed without proper username and passwords. I thought I could use $_SESSION instead of using session_register then use this golbal $_SESSION to protect. Is this right or is there an easier way.

 

cheers in advance

Link to comment
Share on other sites

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
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
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
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
Share on other sites

hello and thank you very much for the advice especially #PFMaBiSmAd

 

@wildteen88 that was a type error on my part so sorry about that

 

anyway it worked like a treat but i think i do need to work on the error pages now, so thanks again

 

laters

 

 

Link to comment
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.