1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Login Page

Discussion in 'PHP' started by buckmajor, Oct 16, 2011.

  1. #1
    Hi there

    What's the best way to create a login page?

    I want to add the basic 'username' and 'password' field and a 'submit' button to my web page.

    * login page - once I someone logs in then they can view the closed off web page.

    Many thanks in advance
    CHEERS :)
     
    buckmajor, Oct 16, 2011 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    JohnnySchultz, Oct 17, 2011 IP
  3. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Hi JS,

    Is there an example on how and where to add that php code?
     
    buckmajor, Oct 17, 2011 IP
  4. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #4
    When you want to secure a page in PHP all you need to do is use sessions. A basic protection without using a database would be this code:

    Login.php
    
    <?php
    ob_start();
    session_start();
    # Set login username
    $username = 'Username Here';
    # Set login password
    $password = 'Password here';
    # Set protected area url
    $protected = 'Protected area page';
    # Do not edit below
    if(!isset($_SESSION['logged_in'])) {
    // Display the HTML login form
        if(!isset($_POST['login'])) {
    ?>
    <form action="" name="login" method="post">
    <p>Username: <input type="text" name="username"/></p>
    <p>Password: <input type="password" name="password"/></p>
    <p><input type="submit" name="login" value="Login"/></p>
    </form>
    <?php
         }else{
          if($_POST['username']!=$username||$_POST['password']!=$password) {
               echo '<p>Wrong password</p>';
               echo '<p><a href="login.php">Continue.</a></p>';
              }else{
              # Set session
              $_SESSION['logged_in']=1;
              # Redirect to protected area
              header('Location:'.$protected);
              }
         }
    }else{
         echo '<p>You are already logged in, <a href="login.php?b=logout">logout</a>.</p>';
    }
    # Logout
    if($_GET['b']=='logout') {
         session_destroy();
         header('Location:login.php');
    }
    ?>
    
    PHP:
    I just wrote this in the quick reply, haven't tested it but all you need to do is copy this code and paste it into a file and save it as login.php

    When you want to secure a page add this code to the very top of the document.

    
    ob_start();
    session_start();
    if(!isset($_SESSION['logged_in'])) {
          header('Location:login.php');
    }
    
    PHP:
    Hope this helps you to get what you need, there's a great deal of room for improvement in this but it should get you started.

    Thanks,
    Glen
     
    HuggyEssex, Oct 17, 2011 IP
  5. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Hey Huggy,

    I tried for hours and still couldn't get it right (frustrating!!). Will keep searching until I find a solution.

    Thanks
     
    buckmajor, Oct 17, 2011 IP
  6. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #6
    What was the problem you had with this?
     
    HuggyEssex, Oct 17, 2011 IP
  7. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Hey Huggy,

    I forgot to mention that I was using it inside a CMS. But its OK now, I found a module that works, well only half of it :D.

    Thanks for the help though :)
     
    buckmajor, Oct 17, 2011 IP
  8. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #8
    No problem!
     
    HuggyEssex, Oct 18, 2011 IP
  9. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #9
    you can just insert this line above each php page..
    
    <?php
    $username = 'username';
    $password = 'password';
    if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || (($_SERVER['PHP_AUTH_USER'] != $username) && ($_SERVER['PHP_AUTH_PW'] != $password))) {
        header('WWW-Authenticate: Basic realm="My Realm"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Text to send if user hits Cancel button';
        exit;
    } else {
        echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
        echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
    }
    ?>
    
    PHP:
    from there you can customize it..
     
    JohnnySchultz, Oct 18, 2011 IP
  10. DavidWincent

    DavidWincent Peon

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    The code is not having errors. Try to find out the mistake you are doing. All the best.
     
    DavidWincent, Oct 18, 2011 IP