SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: PHP and cookies

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Oct 2000
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, I am trying to figure out to use Cookies with PHP. I want it so users can add ratings and comments to things after they have logged in.
    Are there any good articiles or tutorials that will help me.
    I tried:
    http://www.zend.com/zend/tut/session.php
    but I only understand a small part of it, for it's for advanced users...and well...I am not...
    Thanks.

  2. #2
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check the tutorial @ webmonkey.com out. Its good for beginners. Also, take a peal at php.net. Go through the manual, for the setcookie() function. Here is the link http://www.php.net/setcookie
    i dunno...

  3. #3
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cookies,

    These are great for making a user sign in, or other things of that nature... however, probably the easiest way to make sure a user has "signed in" is to require a password before a person can add comments/ratings... for example you would have a drop down box for someone to rate... then right below it you can have two fields... one text for username and one password for their password. In order to make it easier on them... you could make a "sign in" page which would have simple username/password verification, and if passed set a cookie... if not display error, and ask them to try again. Then you could make a sign out page which would allow the user to "sign out", or delete the cookies which hold their username and password.

    Here are some examples:

    Login Script:
    PHP Code:
    <?php
    // Connect to database here.

    // yourdomain name without the [url]http://[/url] or [url]www.[/url]
    $domain "yourdomain.com";
    // login script
    if($submit == "login"){

    $sql "SELECT * FROM users WHERE username = '$username' and password = '$password'";
    $result mysql_query($sql);
    if(
    mysql_num_rows($result) == 1){

    // Set for three hours
    setcookie ("username"$usernametime()+(3600*3), "/"".$domain"0);
    setcookie ("password"$passwordtime()+(3600*3), "/"".$domain"0);

    echo(
    "Logged In");
    } else {
    echo(
    "Wrong username/password");
    }

    if(
    $action == "login"){
    ?>
    <form action="<?php echo $PHP_SELF?>">
    enter username: <input type="text" name="username"><br>
    enter password: <input type="password" name="password"><br>
    <input type="submit" name="submit" value="login"> <input type="reset">
    </form>
    <?php
    }
    ?>
    Log out script:
    PHP Code:
    <?php
    // Sets BACK for one week
    setcookie ("username",, time()-(3600*24*7), "/"".$domain"0);
    setcookie ("password",, time()-(3600*24*7), "/"".$domain"0);

    echo(
    "Logged Out");
    ?>
    Now for a rating script:
    PHP Code:
    <?php

    $username_var 
    "$HTTP_COOKIE_VARS[username]";
    $password_var "$HTTP_COOKIE_VARS[password]";

    if(
    $action == "rate"){
    ?>

    <form action="<?php $PHP_SELF?>">
    <select name="rate">
    <option>1
    <option>2
    <option>3
    <option>4
    <option>5
    </select><br>
    username: <input type="text" name="username" value="<?php echo("$username_var"); ?>"><br>
    password: <input type="password" name="password" value="<?php echo("$password_var"); ?>"><br>
    <input type="submit" name="submit" value="rate"> <input type="reset">
    </form>
    <?php
    }

    if(
    $submit == "rate"){

    $sql "SELECT * FROM users WHERE username = '$username' and password = '$password'";
    $result mysql_query($sql);
    if(
    mysql_num_rows($result) == 1){

    // Do rating function

    } else {

    echo(
    "Wrong username/ password")
    }

    }
    ?>
    There! That should get you going... if you have any problems, just let us know...
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2000
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    exbabylon,
    That's just about 100% of what I want....
    That article looks great too...You guys are great

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or the sessions approach

    Login Script:


    PHP Code:
    <?php
    session_start
    ();
    session_register("SESSION");

    // Connect to database here.

    // yourdomain name without the <a href="http://" target="_blank">[url]http://[/url]</a> or <a href="http://www." target="_blank">[url]www.[/url]</a>
    $domain "yourdomain.com";
    // login script
    if($submit == "login"){

    $sql "SELECT * FROM users WHERE username = '$username' and password = '$password'";
    $result mysql_query($sql);
    if(
    mysql_num_rows($result) == 1){

    // Set for three hours
    $SESSION["username"] = $username;
    $SESSION["password"] = $password;
    session_register($SESSION["username"]);
    session_register($SESSION["password"]);
    echo(
    "Logged In");
    } else {
    echo(
    "Wrong username/password");
    }

    if(
    $action == "login"){
    ?>
    <form action="<?php echo $PHP_SELF?>">
    enter username: <input type="text" name="username"><br>
    enter password: <input type="password" name="password"><br>
    <input type="submit" name="submit" value="login"> <input type="reset">
    </form>
    <?php
    }
    ?>
    Log out script:

    PHP Code:
    <?php
    session_start
    ();
    session_unset();
    echo(
    "Logged Out");
    ?>
    Now for a rating script:

    PHP Code:
    <?php

    session_start
    ();
    if (
    $SESSION["username"] == ""header("Location: login.php");

    if(
    $action == "rate"){
    ?>

    <form action="<?php $PHP_SELF?>">
    <select name="rate">
    <option>1
    <option>2
    <option>3
    <option>4
    <option>5
    </select><br>
    username: <input type="text" name="username" value="<?=$SESSION["username"]?>"><br>
    password: <input type="password" name="password" value="<?=$SESSION["password"]?>"><br>
    <input type="submit" name="submit" value="rate"> <input type="reset">
    </form>
    <?php
    }

    if(
    $submit == "rate"){

    $sql "SELECT * FROM users WHERE username = '$username' and password = '$password'";
    $result mysql_query($sql);
    if(
    mysql_num_rows($result) == 1){

    // Do rating function

    } else {

    echo(
    "Wrong username/ password")
    }

    }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2000
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the advantage of the 2nd (sessions) approach?


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
  •