TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-21-2008, 10:33 AM   #1 (permalink)
The Wanderer
 
Hudson's Avatar
 
Join Date: Sep 2008
Posts: 8
Thanks: 1
Hudson is on a distinguished road
Default Securing area of site

Hello all, hope everyone is well.

I need to password protect an area of my site which contains multiple pages.

I've sorted out the log in system without problem, but I now need to think about how to check to see if a user has logged in.

For example, I don't want people to be able to go directly to www.mysite.com/secretarea/apage.html so I'm guessing I'd need something in each page of the secret area which checks to see if the user has come via the log in, or simply navigated straight there. If it's the latter, I need to kick them back to the log in form.

Does that all make sense? I'm sure this is a real n00b question, but I could do with a pointer in the right direction.

Thanks muchly all
Hudson is offline  
Reply With Quote
Old 10-21-2008, 12:29 PM   #2 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Simple Check for a logged in session:

<?PHP
if(ISSET($_SESSION['user_name']))
{
// Donīt perform any action cause a Logged in session is detected
} else {
// Send the snooping people back that has not logged in
header("Location: index.php");
exit;
}
?>
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
The Following User Says Thank You to EyeDentify For This Useful Post:
Hudson (10-21-2008)
Old 10-21-2008, 01:00 PM   #3 (permalink)
The Wanderer
 
Hudson's Avatar
 
Join Date: Sep 2008
Posts: 8
Thanks: 1
Hudson is on a distinguished road
Default

Ah, I thought I might need to do something with sessions. It's not an area I've looked into much before, but you've definitely started me off in the right direction.

Many thanks.
Hudson is offline  
Reply With Quote
Old 10-21-2008, 09:30 PM   #4 (permalink)
La Vida es Sueņo
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

We do have a good article on sessions. Well, I say good. I wrote it so I may be being a little conceited!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-22-2008, 08:32 AM   #5 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

@Hudson

Iīm just glad i could help. I know when i myself started venturing out into logged in areas of websites and secure things that SESSIONS at first seemed a little scary.

And Wildhoneys article is a good read. i recomend it. :)

Ciao.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 10-22-2008, 08:38 AM   #6 (permalink)
The Wanderer
 
Hudson's Avatar
 
Join Date: Sep 2008
Posts: 8
Thanks: 1
Hudson is on a distinguished road
Default

Yeah, it's an area I've been meaning to get into for a while, but I've never had a "real life" project that needed it (until now).

I'm reading through that article right now. Very useful for a novice such as myself.

One question - can you check to see if multiple parts of the session have been set?

For example...

PHP Code:
<?php

session_start
();

// Check the session to see if it has been set
if (isset($_SESSION['sess_userName']) &&
isset(
$_SESSION['sess_passWord']) &&
isset(
$_SESSION['sess_niceName']) &&
isset(
$_SESSION['sess_sessionId'])) {
// Do not need to do anything as the user is logged in
}

else {
header("Location: ../index2.php"); // Redirect to the log in if the session is not set
exit;
}

?>
When someone logs in it works fine, but when I destroy the session and try to navigate back to the secret area, it doesn't bounce me to ../index2.php (as it should do), but instead just displays a blank page.
Hudson is offline  
Reply With Quote
Old 10-22-2008, 08:42 AM   #7 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Yes you can. But then all the Variables that are tied together with AND would have to be SET to so that first part of the IF clause to become TRUE.

If all else fails. Trial and Error you know :)
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 10-22-2008, 08:45 AM   #8 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

If it does not bounce you back then some SESSION variables still are set.

use this code in somewhere to echo out the session array:

<?PHP

echo('<pre>');
print_r($_SESSION);
echo('</pre>');

?>
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 10-22-2008, 08:47 AM   #9 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

And i forgott.

You should not put your users password in a SESSION variable unless you have a very strong reason and a way to encrypt it so if it ends up in someone elses hands they have no use for it.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 10-22-2008, 08:51 AM   #10 (permalink)
The Wanderer
 
Hudson's Avatar
 
Join Date: Sep 2008
Posts: 8
Thanks: 1
Hudson is on a distinguished road
Default

Mmmmm, session variables are all set correctly, changed it so I'm only checking the $_SESSION['sess_sessionId'] variable and when I output the session variables, it's coming out OK.

Still won't bounce back to ../index2.php. Very odd. I'll dig a little deeper.

I'll remove the password session variable as well.

Cheers for all the help. Much appreciated
Hudson is offline  
Reply With Quote
Old 10-22-2008, 01:53 PM   #11 (permalink)
The Wanderer
 
Hudson's Avatar
 
Join Date: Sep 2008
Posts: 8
Thanks: 1
Hudson is on a distinguished road
Default

Argh, this is getting annoying now.

I'm trying to bounce users back to ../index2.php if they come to the secure area without logging in

At the top of my .com/secure/index.php file I've got an include that pulls in the following file to check if a user is authorised

PHP Code:
<?php
session_start
();

ini_set('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);

    
// Check the session to see if it's been set
    
if (isset($_SESSION['sess_userName'])) {
        } 
// Don't do anything cos the user has logged in.
        
else {
            
header("Location: ../index2.php");
            exit;
        }
?>
I'm getting an error which says (obviously edited certain info )

Quote:
Warning: Cannot modify header information - headers already sent by (output started at /***/***/***/***/***/***.com/***/***/secure/index.php:1) in /***/***/***/***/***/***.com/***/***/includefolder/checkauth.php.inc on line 11
Now then, a quick Google of this error reveals that it's usually caused by content (either real content or whitespace) being sent to the browser before the header() function.

However, I'm calling this include at the very start of the secure/index.php page like this

HTML Code:
<?php include("../includefolder/checkauth.php.inc"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
So as far as I can see there's no whitespace in there. I've also removed any whitespace from the checkauth.php.inc file without any luck.

I've spent the best part of the day trying to sort this, and I can't figure it out for the life of me.

Any help would be appreciated. Ta.
Hudson is offline  
Reply With Quote
Old 10-22-2008, 02:30 PM   #12 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

try putting this at the very top of your PHP:

<?PHP
ob_start();
?>

And put:

<?PHP
ob_end_flush();
?>

Att the very bottom of your PHP page.

Itīs a way to get around the error.

To get an idea about why the error happens. check this link:
PHP: header - Manual

Hope it helps.

/Eye.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 10-22-2008, 02:37 PM   #13 (permalink)
The Wanderer
 
Hudson's Avatar
 
Join Date: Sep 2008
Posts: 8
Thanks: 1
Hudson is on a distinguished road
Default

Mmm, tried output buffering with no joy (forgot to mention that in my previous post - sorry).

I've checked the session variables, and it's nothing to do with them (they're all being created fine, and all destroyed when the user logs out). However, after logging out (which destroys the session variables) and going to .com/secure I get a blank page (or the error if error reporting is turned on) instead of being bounced to the ../index2.php page.
Hudson is offline  
Reply With Quote
Old 10-22-2008, 06:04 PM   #14 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

...you could also go with a .htaccess password protection...

.htaccess authentication - Google Search
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 10-23-2008, 06:35 AM   #15 (permalink)
The Wanderer
 
Hudson's Avatar
 
Join Date: Sep 2008
Posts: 8
Thanks: 1
Hudson is on a distinguished road
Default

I initially thought of using htaccess, but they want a login area as part of the design, rather than an alert appearing when you try to go to .com/secure

AKAIK you can't tie a <form> into htaccess (although someone feel free to correct me if I'm wrong)
Hudson is offline  
Reply With Quote
Old 10-23-2008, 02:25 PM   #16 (permalink)
The Wanderer
 
Hudson's Avatar
 
Join Date: Sep 2008
Posts: 8
Thanks: 1
Hudson is on a distinguished road
Default

I've decided to go a different route.

Instead of sending users to the index page when they try and log in without a username/password, I'm now sending them to a page which tells them the area they're trying to get into is restricted and they need to log in.

On reflection, I think that's probably better from a usability point of view - makes it obvious to the user that they've done something they're not allowed to do, rather than just loading a normal page which looks similar to the secure area.

Thanks for all the help though guys and gals. I've learnt so much about PHP this week. Feels rather good.
__________________
www.veryconscious.com
Hudson is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:48 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design