+ Reply to Thread
Results 1 to 7 of 7

Thread: Need to fix a problem with a tutorial.

  1. #1
    VDC Team Member
    Join Date
    Feb 2004
    Location
    Kentucky
    Posts
    50

    Need to fix a problem with a tutorial.

    This code right here;

    PHP Code:
    <?php

    // Session start
    session_start();

    // Checks password and username
    if($username == "test" AND $password == "1337") {
            
    // Session userid and loggedin is true
        
    $_SESSION[username] = "test"
        
    $_SESSION[userid] = 1;
        
    $_SESSION[loggedin] = 1;
        echo 
    "<font face='verdana' size='1'>You are now logged in as 'test'</font>";

    } else {


            echo 
    "You need access to view this page!"
        
    exit;

    }


    php?>
    does not work. Reason being, the script has NO idea what $username or $password is. To fix this, you need to add the following:

    PHP Code:
    <?php

    // Session start
    session_start();
    $username $_POST[username];
    $password $_POST[password];
    // Checks password and username
    if($username == "test" AND $password == "1337") {
            
    // Session userid and loggedin is true
        
    $_SESSION[username] = "test"
        
    $_SESSION[userid] = 1;
        
    $_SESSION[loggedin] = 1;
        echo 
    "<font face='verdana' size='1'>You are now logged in as 'test'</font>";

    } else {


            echo 
    "You need access to view this page!"
        
    exit;

    }


    php?>
    Unless I missed something, this should do it.

    BTW, if you want to make this for multiple users, and continue to not use SQL, you can just keep adding like so:

    PHP Code:
    <?php

    // Session start
    session_start();
    $username $_POST[username];
    $password $_POST[password];
    // Checks password and username
    if ($username == "test" AND $password == "1234") {
            
    // Session userid and loggedin is true
        
    $_SESSION[username] = "admin"
        
    $_SESSION[userid] = 1;
        
    $_SESSION[loggedin] = 1
        
    header('Location: admin/index.php');
        
    } elseif (
    $username == "joshua" AND $password == "1234") {
        
    $_SESSION[username] = "Joshua";
        
    $_SESSION[userid] = 2;
        
    $_SESSION[loggedin] = 2;
        
    header('Location: users/draketech/index.php');
    } else {


            
    header('Location: loginfailed.php');
        exit;

    }



        
    ?>
    And when you apply it to your pages, just change

    PHP Code:
    session_start();
    if (
    $_SESSION['loggedin'] == 1) { 
    to

    PHP Code:
    session_start();
    if (
    $_SESSION['loggedin'] == whatever number that user is.) { 
    Last edited by jd-inflames; 02-05-2009 at 06:25 PM.
    Zombie Survival Guides | Zombie Game Reviews | Zombie Movie Reviews
    Drake Technologies Forums - What do you know?
    Drake Technologies Partnership Program - Ever wanted your own website?


    Cold was my soul
    Untold was the pain
    I faced when you left me
    A rose in the rain....
    So I swore to the razor
    That never, enchained
    Would your dark nails of faith
    Be pushed through my veins again

  2. #2
    Administrator missionsix's Avatar
    Join Date
    Feb 2004
    Location
    Seattle, WA
    Posts
    719

    Re: Need to fix a problem with a tutorial.

    sometimes global variables are turned on so $username & $password are automatically read from $_POST. However, it is right to assume that these variables need to be assigned before checking against them.

    Your multiple user account system works, but $_SESSION['loggedin'] is really a boolean type value (should have been TRUE / FALSE in the example) and your just checking if a user is logged in, not whether or not they have a certain loggedin 'id'.

    If you want to restrict access based on which use is where, you need some sort of way to group users, and then to restrict access to pages based on their group id.

    I have put some thought into writing an article / tutorial on how to create an authentication system and publish it on missionsix.net but I can't seem to find the time to do it. School is a top priority right now.

  3. #3
    VDC Team Member
    Join Date
    Feb 2004
    Location
    Kentucky
    Posts
    50

    Re: Need to fix a problem with a tutorial.

    Well, I had tried using the script piece by piece how it was written in the tutorial, and it didn't work until I assigned the variables.

    For the help you've given me, I could write some tutorials for you again somewhat like I did with the introductions to php/mysql if you'd like.
    Zombie Survival Guides | Zombie Game Reviews | Zombie Movie Reviews
    Drake Technologies Forums - What do you know?
    Drake Technologies Partnership Program - Ever wanted your own website?


    Cold was my soul
    Untold was the pain
    I faced when you left me
    A rose in the rain....
    So I swore to the razor
    That never, enchained
    Would your dark nails of faith
    Be pushed through my veins again

  4. #4
    registered.
    Join Date
    Apr 2009
    Posts
    1

    Just saying hello (first post)

    Howdy,

    I've been reading for a while and finally decided to sign up. I just wanted to say hi to everyone here.

    Lots of good stuff here. I hope I can give a little back. Sorry if this is in the wrong board, I'm still a little new to this.

  5. #5
    Ping Island AtomicHype's Avatar
    Join Date
    Apr 2005
    Location
    Everett, WA
    Posts
    655

    Re: Need to fix a problem with a tutorial.

    My powers are worth nothing here.

  6. #6
    Administrator missionsix's Avatar
    Join Date
    Feb 2004
    Location
    Seattle, WA
    Posts
    719

    Re: Need to fix a problem with a tutorial.

    if that happens next time you could report the post and i'd get a notification.... or just wait till i find it.

  7. #7
    Ping Island AtomicHype's Avatar
    Join Date
    Apr 2005
    Location
    Everett, WA
    Posts
    655

    Re: Need to fix a problem with a tutorial.

    I didn't even think about that.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. PHP tutorial request Thread:
    By missionsix in forum Website Design
    Replies: 30
    Last Post: 09-01-2008, 05:37 AM
  2. Need Tutorial for signature
    By jtck in forum Request Content
    Replies: 2
    Last Post: 10-02-2007, 01:36 AM
  3. Guest tutorial RULES
    By missionsix in forum User Submitted Tutorials
    Replies: 5
    Last Post: 03-06-2005, 06:22 PM

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