How to password protect the script

Discussion in 'PHP' started by pr-biztech, Jun 11, 2011.

  1. #1
    I have one mailbomber script

    When i hit my web address/mail.php is automatically open


    But now i want to password protect

    Mean when i click my website/mail.php it should ask username and password


    How to do that?
     
    pr-biztech, Jun 11, 2011 IP
  2. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Assuming you have enough HTML and PHP knowledge (Syntax wise) -

    1) Set the user name and password in database or variable
    2) Create a login form
    3) Get the form data using $_POST global in php
    4) Check if supplied user credentials are same as stored info
    5) if yes then grant access, if no then redirect user to login page

    A very simple Login structure would go like this :
    
    // login.php
    <?php
    session_start();
    // Stored User crendentials
    $userName = "User";
    $password = md5("Password");
    if(isset($_POST['userName'])){
        // capture the login data
        $suppliedUserName = $_POST['userName'];
        $suppliedPassword = md5($_POST['password']);
    
        // Check if supplied information is same as stored information
        if(($suppliedUserName == $userName) && ($suppliedPassword == $password)){
            // Info matched grant access
            $_SESSION['isLoggedin'] = true;
        }
        else{
            // Info did not match show error
            $error = "The user name or password did not match";
        }
    }
    ?>
    
    // access.php
    <?php
    session_start();
    if($_SESSION['isLoggedin']){
        // user is logged in, grant him access
    }
    else{
        // user is not logged in, redirect him to login page
    }
    ?>
    
    // mail.php
    <?php
    require_once 'access.php';
    
    // rest of your code goes here
    ?>
    
    PHP:

    I dont suggest you to copy/paste this code. You usually want to filter user input to prevent XSS and SQL injection attempts. I also dont suggest you to store the login info in variables, store them in database instead.
     
    The Webby, Jun 11, 2011 IP
  3. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Does it need to be username and password protection? IP address protection is very easy:
    if($_SERVER['REMOTE_ADDR'] != "192.168.0.1"){
    die('You are not authorised to view this page!');
    }
    PHP:
    Have that before any other code and away you go. Obviously replace that IP address with your own or a selection of IP addresses if you know the static addresses you'll be connecting from.

    If anyone compromises his PHP script (thus hosted files) they most likely have access to his database credentials so I think that's an overly-complicated solution... unless he truly needs multiple users and passwords accessing the same script.
     
    Alastair Gilfillan, Jun 11, 2011 IP
  4. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Yes, IP address based access would be relatively easier, but remember, its very easy to fake your IP address, thus bypassing this protection.

    It's just what I suggest, its certainly up to him what he finds best for his needs.
     
    The Webby, Jun 11, 2011 IP
  5. AppleH

    AppleH Peon

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Login to your cpanel and set password folder.
     
    AppleH, Jun 11, 2011 IP
  6. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    could just use htpasswd
     
    themullet, Jun 11, 2011 IP
  7. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #7
    I don't know much about IP-spoofing I'll admit but I don't understand how the recipient can recieve data that's being sent to a spoofed address; wouldn't they need to've compromised the network or one of its routes so that the impostor could recieve data as well as submit requests?

    Yep; understood. There are lots of good ideas here for the O.P. to choose from based on their needs/skills. :)
     
    Alastair Gilfillan, Jun 12, 2011 IP
  8. pr-biztech

    pr-biztech Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks for help. I am much impressed by your knowledge and I think you are good helper here.
     
    pr-biztech, Jun 13, 2011 IP
  9. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Glad to help :)
     
    The Webby, Jun 13, 2011 IP
  10. pr-biztech

    pr-biztech Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I think you have good knowledge and you are good qualified person.
     
    pr-biztech, Jun 14, 2011 IP