SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: cookie question

  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cookie question

    Can someone tell me why I cannot retrieve and print cookie value with these two scripts :

    to set the cookie :

    PHP Code:

    <form action="" method="post" name="frmLogin" id="frmLogin">
    <table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
    <tr>
    <td width="150"><p class="fields">Korisnik</p></td>
    <td><input name="txtUserId" type="text" id="txtUserId"></td>
    </tr>
    <tr>
    <td width="150"><p class="fields">Lozinka</p></td>
    <td><input name="txtPassword" type="password" id="txtPassword"></td>
    </tr>
    <tr>
    <td width="150" valign="baseline"><p class="fields">Kontrolno polje</p></td>
    <td valign="baseline"><input name="txtNumber" type="text" id="txtNumber" value=""></td>
    </tr>
    <tr>
    <td></td>
    <td valign="baseline"><img src="randomimage.php"></td>
    </tr>

    <tr>
    <td width="150">&nbsp;</td>
    <td><input name="btnLogin" type="submit" id="btnLogin" value="Uloguj" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px"></td>
    </tr>
    </table>
    </form>
    <?
     $user_id 
    $_GET['txtUserId'];
    // $_SESSION['user_old'] = $user_id;
    setcookie('username''$user_id');
    ?>
    retrieve and print :

    PHP Code:
    <?
    // session_start();
    // $user = $_SESSION['user_old'];
    $user $_COOKIE['username'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <link rel="stylesheet" href="style/style.css" />
    </head>
    <body>

    <table width="100%" cellpadding="4" cellspacing="4" border="1">
    <td><a href="ponude.php"><input type="button" id="button" value="Ponude" /></a></td>
    <td><a href="fakture.php"><input type="button" id="button" value="Fakture" /></a></td>
    <td><a href="predracun.php"><input type="button" id="button" value="Predračuni" /></a></td>
    <td><?  print ($user); ?></td>
    </table>
    </body>
    </html>
    Thank you in advance!

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    setcookie('username', '$user_id');

    Single quotes are not evaluated for variables.

    Try: setcookie('username', $user_id);

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, i've tried setcookie('username', $user_id); but still no output after calling
    PHP Code:
    <?
    $user 
    $_COOKIE['username'];
    print (
    $user); ?>

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
    More...

    In other words, you can't have setcookie() below your html code, like in your example.
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  5. #5
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There must be another mistake somewhere between the lines, I've put the function call setcookie at the very beginning of the script with the same result...

    Full time ADMIN - art community
    Part time coder - dsign

  6. #6
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you have a page where people are logging in. They have to enter their user id and password, but their user id is already in $_GET?

    put setcookie() on the second (retrieve) page and use the $_POST value to print the user on that page. On all subsequent pages you can use the cookie value.

  7. #7
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm getting confused with these cookies....

    I've tried to test them as simple as possible :

    1. set the cookie in login.php :

    PHP Code:
    <?php
    setcookie
    ('username''viktor');
    ?>
    2. checked in Firefox-->Options-->Cookies : there's a pair of
    cookie - name : username
    - content : viktor

    3. tried to print out the cookie on members.php:

    PHP Code:
    <?  echo $_COOKIE['username']; ?>
    4. There's a white space in the browser where the cookie is supposed to be!!


    ???

    Full time ADMIN - art community
    Part time coder - dsign

  8. #8
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this real quick and see:

    PHP Code:
    setcookie("username""viktor"time()+60*60*24*30); 
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  9. #9
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems to be OK now. However....
    I want to replace the value "viktor" with dynamic value, inserted by the client. Namely, user will put its user name in a text field which will be the cookie value like

    PHP Code:
    setcookie('username'$user_idtime()+60*60*24*30);
    $user_id $_POST['txtUserId']; 
    but this way it won't work.... Is this possible to solve?

    Full time ADMIN - art community
    Part time coder - dsign

  10. #10
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about first assigning value to variable and only then using that variable?
    Code php:
    $user_id = $_POST['txtUserId'];
    setcookie('username', $user_id, time()+60*60*24*30);

  11. #11
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wish this could be as simple
    but it's not.

    Doesn't work that way....

    Full time ADMIN - art community
    Part time coder - dsign

  12. #12
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you want to set cookie or read from cookie?
    I understood that you wanted:
    User enters his username in text field and sends for processing to server, server reads $_POST['username'] and sets cookie value to this username (which is subsequently sent back to client). Did I understand correctly?

  13. #13
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You understood correctly. And the problem is solved. Thank you very much!

    Full time ADMIN - art community
    Part time coder - dsign

  14. #14
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What was the solution?


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
  •