SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The Idea behind PHP authentication (Newb seeking answers)

    I m designing a small web application that displays information that users input. Of course it requires user to register first, then their information would get displayed. The user infos are stored in a MYSQL database, and it will get called on a public page (only editting or adding info needs registeration)

    The main thing I m confused about is, how to differentiate (differ? distinguish?) between public page and login-required pages. Would it be the same php script file but with some if...else that checks for logins?

    I dont make sense at all

    Since I started learning about PHP 2 weeks ago, I still unsure how this will work. If you have any tutorials or tips. Please post

  2. #2
    SitePoint Enthusiast GhostGambler's Avatar
    Join Date
    Apr 2007
    Location
    Germany, NRW
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    function require_login() {
      if (!isset(
    $_SESSION['logged_in'])) {
        echo 
    "login-form";
        die();
      }
    }

    require_login();
    outsource the function into a global file and then just call it whenever a login is required ... can be done a lot more beautiful of course, but that's it actually~

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Chicago
    Posts
    542
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not certain why you would use die() in the case a user isn't logged in. It makes more sense to continue the "if" statement and call an alternative function in "else", like displaying whatever needs to be displayed for logged in users.

  4. #4
    SitePoint Enthusiast GhostGambler's Avatar
    Join Date
    Apr 2007
    Location
    Germany, NRW
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This function is for a site which as a whole requires the user to be logged in.

    So you could do something like
    PHP Code:
    <?php
    include global.inc;
    require_login();
    echo 
    the real content of the sitejust for logged-in users;
    With the die in the function you assure, that the rest of the code is not executed - it's the most easiest way I think~

    If you just want to leave out some things for not logged in users or sth. like that, you can just use the good old if
    PHP Code:
    <?php
    echo "entry"
    if ($_SESSION['logged_in']) echo edit-link;
    echo 
    "next entry";

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kidwithshirt View Post
    Would it be the same php script file but with some if...else that checks for logins?
    Yes, that's basically how it works.

    You should be aware, that if you have simple login requirements (Eg. all access or no access to certain pages/subdirectories), you can use the web server to manage authentication. If you use Apache (you probably do) for web server, you just have to put a .htaccess file in the directory, where your private scripts are, and put a few lines in it. Google tutorial htaccess for specifics on how.


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
  •