SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: PHP Cookie help

  1. #1
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Cookie help

    Hi guys!

    Problem:

    My DB table having column username and password.

    ------HTML FROM-------

    <form id="form1" name="form1" method="post" action="myactionfile.php">

    <input name="username" type="text" id="username" />

    <input name="password" type="password" id="password" />
    </form>


    ------HTML FROM-------


    I want to apply checking on all pages for username and password.

    Like:

    if you enter correct username and password, you will be able to see entire page otherwise NOT. I want to do it using cookies and sessions.

    Any idea?

    Thanks in advance

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    When user submit your form, you should check if username/password combination is correct and if so, you can set username or userid into session.

    Then you can check on every page which requires user to be logged it, if that element is set.

    You can for example build a little function for that:
    PHP Code:
    function user_isLogin()
    {
        if(isset(
    $_SESSION["username"]))
            return 
    true;
        else 
            return 
    false;    
    }
    //user_isLogin 
    Then on each page which requires user to be logged in, you should check
    PHP Code:
    if(!user_isLogin())
    {
      
    header("Location:login.php");
    }
    else
    {
      
    //continue with a page

    I hope this helps.. Of course you have to start the session with session_start() on each page, or better to start it in a single file which will get included in all others where you need to use sessions.
    -- Jelena --

  3. #3
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay. but how to store value into $_SESSION["username"])) ?

  4. #4
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $_SESSION["username"] = $username
    -- Jelena --


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
  •