Results 1 to 7 of 7

Thread: Day 19 - Members Area - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 19 - Members Area - Become PHP Expert in 30 days

    Members Area

    Members Area is pages in an application that are only available for logged in users.

    On Day 18, we created login page (login.php). If login was successful, we just show a message.

    PHP Code:
    echo "Password Matched"
    Instead of showing the message, we need user redirected to another page.

    Redirect User To Another Page

    In PHP, you can redirect a user to another page using code

    PHP Code:
    header("Location: URL_HERE"); 
    Lets Test

    hostonnet.php

    PHP Code:
    <?php

    header
    ("Location: http://hostonnet.com");
    Upload the file to your web site. On visiting the file, you will get redirected to URL specified in the code, in our case http://hostonnet.com


    Create Members Only Page

    Now we know how to redirect user to another page/URL.

    Lets create our Members only page, so we can redirect users to this page after successful login.

    tasks.php

    PHP Code:
    <?php

    echo "<h1>Welcome to members only page. This page is under construction.</h1>";
    Since our application is TODO task manager, we named our members page tasks.php

    Edit login.php and redirect user to tasks.php

    Open login.php

    Find

    PHP Code:
    if ($userInfo['password'] == $password) {
        echo 
    "Password Matched";

    Replace with

    PHP Code:
    if ($userInfo['password'] == $password) {
        
    header("Location: tasks.php");
        exit;

    Check if User Logged In

    Currently our tasks.php is simple PHP page. It do nothing other than printing some text.

    What we need to do is make sure only logged in members can see this page.

    How we know if user is logged in ? We can set a COOKIE or SESSION.

    When user login, we set a SESSION variable. On tasks.php page, we check if SESSION is set, if not redirect user to login page.

    So lets edit login.php again

    login.php

    Find

    PHP Code:
    if ($userInfo['password'] == $password) {
        
    header("Location: tasks.php");
        exit;

    Replace With

    PHP Code:
    if ($userInfo['password'] == $password) {
        
    session_start();
        
    $_SESSION['AUTH_USER'] = $userInfo;
        
    header("Location: tasks.php");
        exit;

    tasks.php

    Use following code for tasks.php

    PHP Code:
    <?php

    session_start
    ();

    if (!isset(
    $_SESSION['AUTH_USER'])) {
        echo 
    "User is not logged in<br>";
        echo 
    "<a href=login.html>click here</a> to go to login page";
        exit;
    }

    echo 
    "<h1>Welcome to members only page. This page is under construction.</h1>";
    We check if SESSION is set. If no session set, we show error message and exit program.

    Logging Off User

    Logging off user is simple. Just destroy the session.

    logout.php

    PHP Code:
    <?php

    session_start
    ();
    session_destroy();

    echo 
    "<p>User logged off successfully.</p>";
    Now lets add a log out link from our tasks page.

    Open tasks.php

    Add

    PHP Code:
    echo "<p><a href='logout.php'>Click here to logout</a>"
    to end of the file.




    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/tasks.php

    PHP Code:
     <?php

    session_start
    ();

    if (!isset(
    $_SESSION['AUTH_USER'])) {
        echo 
    "User is not logged in<br>";
        echo 
    "<a href=login.html>click here</a> to go to login page";
        exit;
    }

    echo 
    "<h1>Welcome to members only page. This page is under construction.</h1>"

    echo 
    "<p><a href='logout.php'>Click here to logout</a>";

  3. #3
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/tasks.php

    Code:
    <?php
    
    session_start();
    
    if (!isset($_SESSION['AUTH_USER'])) {
    echo "User is not logged in<br>";
    echo "<a href=login.html>click here</a> to go to login page";
    exit;
    }
    
    echo "<h1>Welcome to members only page. This page is under construction.</h1>"; 
    
    echo "<p><a href='logout.php'>Click here to logout</a>";
    Last edited by sherlyk; 08-13-2014 at 05:24 AM.

  4. #4
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/tasks.php

    PHP Code:
     <?php

    session_start
    ();

    if (!isset(
    $_SESSION['AUTH_USER'])) {
        echo 
    "User is not logged in<br>";
        echo 
    "<a href=login.html>click here</a> to go to login page";
        exit;
    }

    echo 
    "<h1>Welcome to members only page. This page is under construction.</h1>"

    echo 
    "<p><a href='logout.php'>Click here to logout</a>";

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/fwhphp/public_html/annie/tasks.php:1) in /home/fwhphp/public_html/annie/tasks.php on line 3
    User is not logged in
    click here to go to login page
    No white space before <?
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  5. #5
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

  6. #6
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    http://php.flashwebhost.com/stefin/tasks.php

    Welcome to members only page. This page is under construction.


    Click here to logout

    PHP Code:

    <?php
    session_start
    ();
    if (!isset(
    $_SESSION['AUTH_USER'])) {   

    echo 
    "User is not logged in<br>";    
    echo 
    "<a href=login.html>click here</a> to go to login page";  
    exit;
    }

    echo 
    "<h1>Welcome to members only page. This page is under construction.</h1>";
    echo 
    "<p><a href='logout.php'>Click here to logout</a>";
    Last edited by stefin; 08-12-2014 at 04:58 PM.

  7. #7
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

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
  •