+ Reply to Thread
Results 1 to 4 of 4

 

Thread: Best way to secure a directory in php

  1. #1
    accelerator's Avatar
    Online shopping rocks!

    Status
    Offline
    Join Date
    Nov 2004
    Location
    England
    Posts
    3,008
    Thanks
    54
    Thanked 180 Times in 161 Posts


    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.

  2. #2
    D-Mac's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Surrey
    Posts
    1,351
    Thanks
    29
    Thanked 49 Times in 44 Posts
    David Macfarlane
    Cost effective web development. Codewise

  3. #3
    tbp
    Registered User

    Status
    Offline
    Join Date
    Dec 2006
    Posts
    1,998
    Thanks
    0
    Thanked 22 Times in 22 Posts
    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.

  4. #4
    accelerator's Avatar
    Online shopping rocks!

    Status
    Offline
    Join Date
    Nov 2004
    Location
    England
    Posts
    3,008
    Thanks
    54
    Thanked 180 Times in 161 Posts
    Thanks for your replies D-Mac and tbp, much appreciated.

+ Reply to Thread


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Submit your Site to PR4 Web Directory
    By MrCatz in forum Network / Merchant Incentives & Promotions
    Replies: 0
    Last Post: 03-04-08, 05:29 PM
  2. Permanent Backlinks in our Paid Link Directory!
    By MrCatz in forum Media Coverage & PR Strategy
    Replies: 1
    Last Post: 04-02-08, 10:51 AM
  3. PHP Link Directory Script
    By uklejon in forum Programming
    Replies: 1
    Last Post: 15-09-05, 06:06 PM
  4. Anyone anygood with xml, php, mysql and Affiliate Window
    By AnnonnyMouse in forum Affiliate Marketing Lounge
    Replies: 1
    Last Post: 23-09-04, 12:00 PM
  5. PHP or Browser Timeout?
    By Barry in forum Programming
    Replies: 6
    Last Post: 28-04-04, 02:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2