SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2000
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to set a cookie after a user logs in. The login form brings the user to this page where I set the cookie, but it keeps giving me a parse error on the setCookie line. Here's the code, could someone explain to me what I'm doing wrong?

    <?php
    #Connect to database here

    $expire = time() + (3600 * 24 * 180);
    $path = "/";
    $domain = ".mydomain.com";

    $query = "SELECT USERNAME, PASSWORD FROM MEMBERS";
    $query .= " WHERE USERNAME = '$USER' AND";
    $query .= " PASSWORD = '$PWORD';

    $user_result = mysql_query($query);

    $num_users = mysql_num_rows($user_result);

    $memb = 12345;

    if ($num_users == 0)) {
    echo 'Authorization Required!\n';
    echo 'Hit reload to try again.';
    exit;
    } else {

    setcookie ("id", $memb, $expire, $path, $domain, 0);

    }
    ?>

    thanks in advance,
    kankohi

  2. #2
    SitePoint Member
    Join Date
    Jul 2000
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($num_users == 0)) {
    ------------------^^^^----

    There is an extra ")".



    ------------------
    Antti
    Huotari.com/antti/
    LinuxWebDevNews.com

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2000
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I corrected it, but its still giving me a parse error. Do I have to set the cookie before the header? could that be it? and if so, how would I do that?

    here's my full code:

    <html>
    <head>
    <title>User Authentication</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF">
    <?php

    //Connect to database

    $expire = time() + (3600 * 24 * 180);
    $path = "/";
    $domain = ".mydomain.net";
    $memb = 12345;

    $query = "SELECT USERNAME, PASSWORD FROM MEMBERS";
    $query .= " WHERE USERNAME = '$USER' AND";
    $query .= " PASSWORD = '$PWORD';

    $user_result = mysql_query($query);

    $num_users = mysql_num_rows($user_result);

    if ($num_users == 0) {
    echo 'Authorization Required!\n';
    echo 'Hit reload to try again.';
    exit;
    } else {

    #This is where I'm getting the parse error
    setcookie ("id", $memb, $expire, $path, $domain, 0);
    }

    ?>

    </body>
    </html>

    [This message has been edited by kankohi (edited July 30, 2000).]

  4. #4
    SitePoint Member
    Join Date
    Jul 2000
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to set the cookie before any output is sent to the browser. So, (re)move everything before the php tag. BTW, cookies are part of the header.



    ------------------
    Antti
    Huotari.com/antti/
    LinuxWebDevNews.com

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2000
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay, tried it again... still getting a parse error at the "setcookie" function;

    here's how I changed it:

    <?php
    // Connect to database

    $expire = time() + 3600;
    $path = "/";
    $domain = ".mydomain.net";
    $memb = 12345;

    $query = "SELECT USERNAME, PASSWORD FROM MEMBERS";
    $query .= " WHERE USERNAME = '$USER' AND";
    $query .= " PASSWORD = '$PWORD';

    $user_result = mysql_query($query);

    $user_num = mysql_num_rows($user_result);

    if ($user_num == 1) {

    setcookie ("id", $memb, $expire, $path, $domain, 0);

    }

    ?>
    <html>
    <head>
    <title>User Authentication</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF">

    <?php

    echo "Congratulations, you have logged in as $USERNAME";
    echo "<p> You're cookie value is $memb ";

    ?>

    </body>
    </html>

  6. #6
    SitePoint Member
    Join Date
    Jul 2000
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $query .= " PASSWORD = '$PWORD';
    -----------------------------^^^^

    " is missing.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2000
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Antii! Thank you so much!! it worked!!!!


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
  •