How can I use PHP to create login-controlled web pages?

11 replies
How can I use PHP to create login-controlled web pages?
#create #logincontrolled #pages #php #web
  • Profile picture of the author Yadav Poonam
    You can use php sessions for create login-controlled web pages..
    {{ DiscussionBoard.errors[10402979].message }}
  • Profile picture of the author robomedia
    If you want to do it from scratch by yourself, you need to check before each page is displayed if user is already logged in by i.e. checking if some variable in session exists. You also need to create a login form where you collect username and password, this page shouldn't check for this variable as you want not logged users to be able to use it. Then after posting the form you check your database for this user and password. If matches you set the session variable to some value ( i.e. $_SESSION['logged_in_user']=32 ) and redirect to protected page .

    If you want to save time and not reinvent the wheel just use some framework. Depending of what are you building, I would suggest Yii Framework as in my opinion it's easiest to learn.
    {{ DiscussionBoard.errors[10402999].message }}
    • Profile picture of the author Big Squid
      Simplest way:

      1. Create a file called "authorize.php"
      2. Copy & paste the code below into authorize.php
      <?php

      //Username and password for authentication
      $username='ENTER USERNAME';
      $password='ENTER PASSWORD';

      if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
      ($_SERVER['PHP_AUTH_USER'] !=$username) || ($_SERVER['PHP_AUTH_PW'] !=$password)) {
      //The username and password are incorrect so send the authentication headers
      header('HTTP/1.1 401 Unauthorized');
      header('WWW-Authenticate: Basic realm="ENTER REALM"');
      exit('<h2>Not Available</h2>Sorry, you must enter a valid username and password to access this page.');
      }
      ?>
      3. Replace the variables in the code. Variables for username, password, and realm.
      4. Anytime you want a page protected, paste the following at the top of that page:
      PHP Code:
      <?php require('authorize.php'); ?>
      {{ DiscussionBoard.errors[10404754].message }}
  • Profile picture of the author richasharma
    make a session variable for login control and destroy it when logout click.
    $_SESSION['X']=USERNAME
    {{ DiscussionBoard.errors[10406330].message }}
  • {{ DiscussionBoard.errors[10406679].message }}
    • Profile picture of the author SteveJohnson
      Originally Posted by David Beroff View Post

      Always on the lookout for newer/better/easier wasy for PHP authentication, but this one is a little old:
      Accountify is out of date. It was a nice system, but it is based on PHP 4 and Pear:B, both of which are definitely deprecated at this point. If you want to build web applications in PHP in 2012, we recommend that you check out Symfony or Drupal. Symfony is your best bet if you are a programmer, Drupal may work better for you if you are not.
      Signature

      The 2nd Amendment, 1789 - The Original Homeland Security.

      Gun control means never having to say, "I missed you."

      {{ DiscussionBoard.errors[10410118].message }}
      • Profile picture of the author David Beroff
        Originally Posted by SteveJohnson View Post

        Always on the lookout for newer/better/easier wasy for PHP authentication, but this one is a little old:
        Yeah, I actually started working on fixing that a few years ago, and really should circle back to finish the job. It's still a great package, and builds on top of the PHP session idea that others describe above.
        Signature
        Put MY voice on YOUR video: AwesomeAmericanAudio.com
        {{ DiscussionBoard.errors[10410129].message }}
  • Profile picture of the author nbatioco
    Try to watch this one:


    Let me know if it helps. I can also show you the way via skype or PM here.
    {{ DiscussionBoard.errors[10408469].message }}
  • <html>
    <head>
    <title>User Login Form - PHP MySQL Ligin System | W3Epic.com</title>
    </head>
    <body>
    <h1>User Login Form - PHP MySQL Ligin System | W3Epic.com</h1>
    <?php
    if (!isset($_POST['submit'])){
    ?>
    <!-- The HTML login form -->
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    Username: <input type="text" name="username" /><br />
    Password: <input type="password" name="password" /><br />

    <input type="submit" name="submit" value="Login" />
    </form>
    <?php
    } else {
    require_once("db_const.php");
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
    exit();
    }

    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
    echo "<p>Invalid username/password combination</p>";
    } else {
    echo "<p>Logged in successfully</p>";
    // do stuffs
    }
    }
    ?>
    </body>
    </html>
    {{ DiscussionBoard.errors[10408479].message }}
    • Profile picture of the author SteveJohnson
      Originally Posted by conativeitsolutions View Post

      crappy code here
      Where did you copy/paste that crap from?

      Ahhhh, I see - another sig spammer.
      Signature

      The 2nd Amendment, 1789 - The Original Homeland Security.

      Gun control means never having to say, "I missed you."

      {{ DiscussionBoard.errors[10410123].message }}
  • {{ DiscussionBoard.errors[10411047].message }}

Trending Topics