SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Up till now I have been using cookies to pass information from page to page. How can I use sessions to, after using a function to confirm a users identity, would hold their username and password in the session to make available to other pages?

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pretty simple, try this manual: www.php.net/session
    In the login page, you would session_register the username+password if they're correct, then on follow-up pages, call session_start as well as checking the variables (username+passowrd) are registered before (so they're actually passed through login page)
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I have written a simple function that pulls the username and password from an include file. If the passwords match the function returns 1 if they don't, it returns 0.
    So in the main page I use

    if(!(authenticate($username,$password)
    {
    //Show Login Form
    }
    else
    {
    session_register("username",$username);
    session_register("password",$password);
    //Do rest of work
    }

    But I keep getting "Can't send header information" errors! There is nothing being sent to the browser, so why am I getting this?

  4. #4
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check for blank space or ANYTHING before the <? in that php file
    cogito, ergo sum

  5. #5
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    There is no whitespace... Here is the code for the main file.

    <?
    include("kdls_d/pass.inc");
    include("admin_config.inc");

    if(!(authenticate($username,$password)))
    {


    $output = "<HTML><HEAD><TITLE>Administration Login</TITLE></HEAD> <BODY><P><H3>Please Input a correct username and password.</H3><P>Please note that username and password are case sensitive.<P><form action=\"$PHP_SELF\" method=\"post\"><b>Username: </b><input type=\"text\" name=\"username\"><P><b>Password: </B><input type=\"password\" name=\"password\"><P><input type=\"submit\" name=\"submit\" value=\"Login\"></form>";

    echo $output;
    }
    else
    {
    session_register("username",$username);
    session_register("password",$password);

    echo "<HTML><HEAD><TITLE>Administration</TITLE></HEAD><BODY><H1>Administration</H1><P>Choose one of the following.</P>";
    $forms = "<P>Select Nurses By Years of Experience - <form action=\"select_years.php\" method=\"post\"><input type=\"submit\" value=\"SELECT\">";
    echo $forms;
    }
    ?>

    And also the include file (that has the authentication function).

    <?
    function authenticate($username, $password)
    {
    if($username != $adminuser && $password != $adminpass)
    return 0;

    else
    return 1;

    }
    ?>

    I know the form output is messy but this was just a test script. The passwords are kept in a seperate directory in an .inc file.
    Last edited by Hartmann; Feb 12, 2001 at 16:20.

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check the included files for white spaces at the end
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  7. #7
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Ok did what you said and got the sessions to work. However I cannot get the sessions to work exactly like you stated.

    Here is the code:

    index.php
    <?
    include("kdls_d/pass.inc");
    include("admin_config.inc");

    if(!(authenticate($username,$password)))
    {


    $output = "<HTML><HEAD><TITLE>Administration Login</TITLE></HEAD> <BODY><P><H3>Please Input a correct username and password.</H3><P>Please note that username and password are case sensitive.<P><form action=\"$PHP_SELF\" method=\"post\"><b>Username: </b><input type=\"text\" name=\"username\"><P><b>Password: </B><input type=\"password\" name=\"password\"><P><input type=\"submit\" name=\"submit\" value=\"Login\"></form>";

    echo $output;
    }
    else
    {
    session_register($username);
    session_register($password);

    echo "<HTML><HEAD><TITLE>Administration</TITLE></HEAD><BODY><H1>Administration</H1><P>Choose one of the following.</P>";
    $forms = "<P><A HREF=\"index2.php\">Select Nurses By Years of Experience</A>";
    echo $forms;
    }
    ?>

    index2.php
    <?
    include("kdls_d/pass.inc");
    include("admin_config.inc");
    session_start();

    if(!(authenticate($username,$password)))
    {
    echo "Not Logged In - <A HREF=\"index.php\">Click Here To Login</A>";
    }
    else
    {
    $output = "<a href=\"logout.php\">Logout</a>";
    echo $output;
    }

    ?>

    logout.php
    <?
    session_start();
    session_unregister($username);
    session_unregister($password);
    session_destroy();
    ?>

    admin_config.inc
    <?
    function authenticate($username, $password)
    {
    if($username != $adminuser && $password != $adminpass)
    return 1;

    else
    return 0;

    }
    ?>

    The problem that I am getting is that when I click the link to index2.php it tells me that I am not logged in.

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How does your authenticate function get the value of $adminuser and $adminpass? That seems like a good place to start.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    HEHE Freddy, I was looking at my code as I posted it and that thought crossed my mind...... I fixed it. Thanks for the help Sheesh! What was I thinking!


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
  •