Problem setting Cookie Please help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sarah Michael

    Problem setting Cookie Please help!

    Hi,

    I am running the following code to set a cookie but it is not working,
    please
    help me sorting out the problem. The code is from an online article


    I am using Windows 2000 SP5, PHP 4.2.3, MYSQL 4.0.13-nt, apache 2.0.45


    <?php
    $psEmail = $_POST["psEmail"];
    $psPassword = $_POST["psPassword "];
    $psRefer = $_Post["psRefer"];

    // Check if the information has been filled in
    if($psEmail == '' || $psPassword == '')
    {
    // No login information
    header('Locatio n: LoginTest.php?r efer='.urlencod e($psRefer));
    }
    else
    {
    // Authenticate user
    $hDB = mysql_connect(' localhost', 'root', 'sarah');
    mysql_select_db ('TestData', $hDB);
    $sQuery = "Select iUser, MD5(UNIX_TIMEST AMP() + iUser +
    RAND(UNIX_TIMES TAMP())) sGUID From tblUser Where sEmail = '$psEmail'
    And sPassword = password('$psPa ssword')";

    $hResult = mysql_query($sQ uery, $hDB);
    if(mysql_affect ed_rows($hDB))
    {
    $aResult = mysql_fetch_row ($hResult);
    // Update the user record
    $sQuery = "
    Update tblUser
    Set sGUID = '$aResult[1]'
    Where iUser = $aResult[0]";
    mysql_query($sQ uery, $hDB);
    // Set the cookie and redirect
    setcookie("sess ion_id", $aResult[1], time() + 3600)
    }
    else
    {
    // Not authenticated
    header('Locatio n: LoginTest.php?r efer='.urlencod e($psRefer));
    }

    }
    ?>
  • Alvaro G Vicario

    #2
    Re: Problem setting Cookie Please help!

    *** Sarah Michael wrote/escribió (5 Nov 2003 22:13:09 -0800):[color=blue]
    > I am running the following code to set a cookie but it is not working[/color]

    I suggest to try to identify what part of your code doesn't work properly.
    You're mixing form processing, database access, cookies... God knows what
    fails here ;-)


    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --

    Comment

    • Randell D.

      #3
      Re: Problem setting Cookie Please help!


      "Sarah Michael" <sara_michael77 @yahoo.com> wrote in message
      news:2ac61ed8.0 311052213.9f22a 2e@posting.goog le.com...[color=blue]
      > Hi,
      >
      > I am running the following code to set a cookie but it is not working,
      > please
      > help me sorting out the problem. The code is from an online article
      > http://www.devarticles.com/art/1/639
      >
      > I am using Windows 2000 SP5, PHP 4.2.3, MYSQL 4.0.13-nt, apache 2.0.45
      >[/color]
      [snipped]> }[color=blue]
      > ?>[/color]

      Are you getting any error messages (if not, are errors displayed? They could
      be switched off in your php.ini file so check log files instead).

      Use headers_sent() to see if you have managed to send a blank space or
      something to the browser before setting the cookies (thus, in case you
      didn't know - no output to the browser should occur before any headers being
      sent).

      Secondly - I would put an exit after your call to header("Locatio n:"...

      Sometimes I have found problems mixing functions inside a call with the
      header function... thus

      header('Locatio n: LoginTest.php?r efer='.urlencod e($psRefer));

      *maybe* a bad idea...

      Try instead something like

      $tmp="LoginTest .php?refer=".ur lencode($psRefe r);
      header("Locatio n: $tmp");
      exit;

      Lastly... instead of redirecting to "LoginTest. php" try inserting the full
      domain path name (thus use http://www.mydomain.com/LoginTest.php instead of
      just plain LoginTest.php)


      While the above are not directly related to createing cookies, an error in
      code preceeding the setcookie could well be the problem of characters being
      sent to the browser, thus making setcookie fail (since only headers/cookies
      should be sent first).

      If the above doesn't help you - then tell us what error messages you are
      getting...


      Comment

      Working...