+ Reply to Thread
Results 1 to 9 of 9

Thread: PHP setcookie problem...

  1. #1
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    PHP setcookie problem...

    I have a problem within one of my programs where I cannot set cookies at all.

    Code:
    [...]
    if (conditions)
    {
    $cookiedata = '123';
    setcookie("cookie1", "$cookiedata", time()+604800, '/');
    echo "$cookiedata";
    
    }
    [...]
    I have used the above code, and the page will show 123, as expected. However, there is no cookie!

    At first I thought maybe I'm not allowed to set cookies ;)
    so I created a new page that looked like the following:
    Code:
    <?php
    $cookiedata = '123';
    setcookie("cookie1", "$cookiedata", time()+604800, '/');
    echo "$cookiedata";
    ?>
    Again, it outputs the expected 123, but this time it does set a cookie.
    Am I missing something here?

    BTW, no header information was sent prior to the cookie, and PHP gives no error.
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  2. #2
    leafypiggy's Avatar
    leafypiggy is offline Community Advocate leafypiggy is on a distinguished road
    Join Date
    Aug 2007
    Location
    Massachusetts
    Posts
    2,228

    Re: PHP setcookie problem...

    can you please post all of your code for the first script?

    the rest of it might have an answer to your question
    Neil Hanlon | x10Hosting Support Representative
    Neil[at]x10hosting.com
    █ I'm always happy to help. Just ask a question in Free Hosting
    Terms of Service IRC

  3. #3
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: PHP setcookie problem...

    It concerns login to my site and deals with passwords and such. I have commented out anything I don't want publically known that is also irrelevant. As for the actual setcookie part, I uploaded the '123' test, with identical results as to the normal data it has. Namely, what I previously mentioned.
    Code:
    <?php
    function mysql_fetch_rowsarr($result, $numass=MYSQL_BOTH) {
      $got=array();
      mysql_data_seek($result, 0);
      while ($row = mysql_fetch_array($result, $numass)) {
        array_push($got, $row);
      }
      return $got;
    }
    
    require_once("scripts/mysqllogin.php");
    
    if (isset($_GET['page']))
    $page = $_GET['page'];
    else
    $page = 'index';
    
    if (!file_exists("pages/$page.php"))
    $page = 'index';
    
    if (isset($_GET['action']))
    $action = $_GET['action'];
    
    require_once("scripts/login.php");
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    
    [...]
    contents of scripts/mysqllogin.php
    Code:
    <?php
    $database  =  array(
    'url'      => 'localhost'     ,
    'name'     => '*****'       ,
    'pass'     => '*****'      ,
    'database' => 'data'    ,
    'table'    => 'as_nations');
    
    mysql_connect($database['url'],$database['name'],$database['pass']) or die('Error connecting to MySQL: '.mysql_error());
    mysql_select_db($database['database']) or die('Error connecting to MySQL database: '.mysql_error());
    ?>
    contents of scripts/login.php
    Code:
    <?php
    $name = '';
    $pass = '';
    if ($action == 'login')
    {
    if (isset($_POST['name']))
    $name = $_POST['name'];
    $name = strtolower(str_replace(' ','_',$name));
    
    if (isset($_POST['password']))
    $pass = $_POST['password'];
    }
    else
    if (isset($_COOKIE['as_data']))
    {
    $cookie = $_COOKIE['as_data'];
    $cookie = // ;)
    $name   = // ;)
    $pass   = // ;)
    }
    
    $database['table'] = 'as_provinces';
    
    $query = "SELECT id,name,pass,nation,staff FROM {$database['table']} WHERE name='$name'";
    $result = mysql_query($query) or die('Error retrieving data: '.mysql_error());  
    
    $rows = mysql_fetch_array($result);
    $data = array();
    $data['id']     = $rows['id'];
    $data['name']   = $rows['name'];
    $data['pass']   = $rows['pass'];
    $data['nation'] = $rows['nation'];
    $data['staff']  = $rows['staff'];
    
    $pass = crypt($pass, $data['pass']);
    
    if (($action == 'logout') || ($data['pass'] == ''))
    $pass = 'log me out! ;)';
    
    if ($pass == $data['pass'])
    {
    $cookiedata = '123';
    setcookie("cookie1", "$cookiedata", time()+604800, '/');
    echo "$cookiedata";
    
    $loggedin = array('id' => $data['id'], 'name' => $data['name'],'ucname' => ucwords(str_replace('_',' ',$data['name'])), 'nation' => $data['nation'], 'staff' => $data['staff']);
    }
    else
    {
    setcookie("cookie1", "0", 1218000000, '/');
    $loggedin = array('id' => '', 'name' => '','ucname' => '', 'nation' => '', 'staff' => 0);
    }
    
    ?>
    Last edited by Scoochi2; 08-17-2008 at 07:03 PM. Reason: typo ;)
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  4. #4
    leafypiggy's Avatar
    leafypiggy is offline Community Advocate leafypiggy is on a distinguished road
    Join Date
    Aug 2007
    Location
    Massachusetts
    Posts
    2,228

    Re: PHP setcookie problem...

    unless everything else in that script works....

    i'm fairly certain that php overwrites the previous variable if it is the same.

    so, i might actually put the data into an array. and try that.
    Neil Hanlon | x10Hosting Support Representative
    Neil[at]x10hosting.com
    █ I'm always happy to help. Just ask a question in Free Hosting
    Terms of Service IRC

  5. #5
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: PHP setcookie problem...

    Quote Originally Posted by leafypiggy View Post
    unless everything else in that script works....

    i'm fairly certain that php overwrites the previous variable if it is the same.

    so, i might actually put the data into an array. and try that.
    Yeah, it does. And it's something I take advantage of. For example:
    Code:
    if (($action == 'logout') || ($data['pass'] == ''))
    $pass = 'log me out! ;)';
    
    if ($pass == $data['pass'])
    But even if it isn't overwriting a variable, it should still create the cookie, yes?
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  6. #6
    leafypiggy's Avatar
    leafypiggy is offline Community Advocate leafypiggy is on a distinguished road
    Join Date
    Aug 2007
    Location
    Massachusetts
    Posts
    2,228

    Re: PHP setcookie problem...

    it should...but i'm not a PHP expert persay. lol.

    i know the basics...and some of the intermediate/advanced. xD

    wait for someone else? lol. dont know what to tell you. It should work.
    Neil Hanlon | x10Hosting Support Representative
    Neil[at]x10hosting.com
    █ I'm always happy to help. Just ask a question in Free Hosting
    Terms of Service IRC

  7. #7
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: PHP setcookie problem...

    It should... Which is why I'm so confused now, lol!
    Edit:
    Anyone able to help?
    Last edited by Scoochi2; 08-18-2008 at 06:58 PM. Reason: Automerged Doublepost
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  8. #8
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: PHP setcookie problem...

    Yay! I've finally reached a solution.
    or at least a workaround...

    I've moved all my set cookies right to the top of my header file, thus they are now set before it connects to MySQL. I guess MySQL was causing the cookies not to set, without giving an error?
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  9. #9
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: PHP setcookie problem...

    A mySQL connection should not affect cookies, I deal with login scripts a lot. It could possibly be from the variable $cookiedata is enclosed in "quotes". I remember having funky problems like you are having above.

    PS. If you're using Firefox, download Web Developer toolbar if you haven't already. In there, you can view all cookies for a website (and just about every else you can dream of, and some you cant), which would be very useful in this case.

    -xP

+ Reply to Thread

Similar Threads

  1. php problem
    By biso911 in forum Free Hosting
    Replies: 13
    Last Post: 03-16-2008, 02:03 AM
  2. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  3. problem in php mail function and smtp
    By idrees in forum Free Hosting
    Replies: 2
    Last Post: 02-08-2008, 05:16 PM
  4. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM
  5. php ad code problem
    By sourabhj in forum Free Hosting
    Replies: 7
    Last Post: 08-22-2006, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers