Affiliate Marketing
Forum Search

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 05-04-08
accelerator's Avatar
Online shopping rocks!
 
Join Date: Nov 2004
Location: England
Posts: 1,198
Thanks: 5
Thanked 9 Times in 6 Posts
accelerator is an unknown quantity at this point
  Best way to secure a directory in php

Hi All

I need to secure a directory against unauthorised access using php. It's going to be an online admin area for my database, there won't be any really important info like sensitive financial data so I won't need SSL. I would like as straightforward a technique as possible, it is only going to be me that will be accessing it.

Thanks for any suggestions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-04-08
D-Mac's Avatar
Registered User
 
Join Date: Mar 2004
Location: Surrey
Posts: 977
Thanks: 1
Thanked 2 Times in 2 Posts
D-Mac is an unknown quantity at this point
  Re: Best way to secure a directory in php

.htpasswd

htpasswd htaccess - Google Search
__________________
David Macfarlane
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-04-08
tbp tbp is offline
Registered User
 
Join Date: Dec 2006
Posts: 1,917
Thanks: 0
Thanked 10 Times in 10 Posts
tbp is an unknown quantity at this point
  Re: Best way to secure a directory in php

As D-Mac said, .htaccess is a great solution, but some hosts don't allow it.

With PHP, you would need an index.php file in the directory with a form for the user to fill in their username and password (called "username" and "password") and a log in button. I also put a hidden field with a name of "action" and a value of "login".

Make the form post back to itself (ie the same page as the form is on), and add the following to the first line of the page, above the HTML:

PHP Code:
if($_POST['action'] == "login"){
  if(
$_POST['username'] == "myusername" && $_POST['password'] == "mypassword"){
    
$_SESSION['user'] = $_POST['username'];
    
header("Location: protected_page.php";
  }

If the username and password are correct (hard coded in the example above, but normaly pulled from a database), then it will put the username in a session variable called "user" and then redirect to "protected_page.php".

On each page that you want to protect, add the following to the top of each page at line 1 (again above the html).

PHP Code:
if(strlen($_SESSION['user']) < 1){
  
header("Location: index.php");

This checks for the prescence of the user session variable, and only allows the user to view the page if the session variable is set (meaning the user has logged in) otherwise they are redirected to the index page to login.

Simple to setup, but does work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-04-08
accelerator's Avatar
Online shopping rocks!
 
Join Date: Nov 2004
Location: England
Posts: 1,198
Thanks: 5
Thanked 9 Times in 6 Posts
accelerator is an unknown quantity at this point
  Re: Best way to secure a directory in php

Thanks for your replies D-Mac and tbp, much appreciated.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Submit your Site to PR4 Web Directory MrCatz Merchant Promotions & Incentives 0 03-04-08 04:29 PM
Permanent Backlinks in our Paid Link Directory! MrCatz Website Promotion 1 04-02-08 09:51 AM
PHP Link Directory Script uklejon Widgets, Coding, AJAX, PHP - Technology & Affiliate Marketing 1 15-09-05 05:06 PM
Anyone anygood with xml, php, mysql and Affiliate Window AnnonnyMouse The Affiliate Marketing Lounge 1 23-09-04 11:00 AM
PHP or Browser Timeout? Barry Widgets, Coding, AJAX, PHP - Technology & Affiliate Marketing 6 28-04-04 01:04 PM


Affiliate Marketing RSS Feeds - Contact Us - Affiliate Marketing - Archive - Privacy Statement - Top

Content Relevant URLs by vBSEO 3.2.0 RC7