Results 1 to 6 of 6

Thread: Cookie problem

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    Cookie problem

    Hello,

    I am new to PHP (few week) and I cannot write cookie corectly without having message error.

    Here is the error :

    Warning: Cannot add header information - headers already sent by (output started at c:\program files\easyphp\www\nowlogin.php:18) in c:\program files\easyphp\www\nowlogin.php on line 58

    Here is the code of my .php:


    PHP Code:
    <html>

    <head>
    <title>[   P   ] atriot</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <LINK href="patriot.css" rel="stylesheet" type="text/Css">
    </head>

    <body>


    <?php
            
    require_once("header.php");
            
    AfficherEntete();
    ?>

    <?php
    // db variables
    // inserts variables accordingly
    $host "myip";
    $user "allo";
    $pass "bye";
    $db "nothere";
    $table "boo";

        
    //Connection a la base de donnee
        
    $link mysql_connect ($host$user$pass)
                or die(
    "Impossible de se connecter : " mysql_error());
        
    //Selection de la base utilisee        
         
    mysql_select_db($db);
        
        
    //Prend les parametres 
        
    $varId=$_POST['User_id'];
        
        
    //Prend le numéro de de l'équipe
        
    $sql 'SELECT usagers_nom FROM usagers WHERE usagers_id = '  .$varId;
        
        
    //Execute l'ordre sql
        
    $result mysql_query($sql)
            or die (
    "NO USER FOUND");
            
        
    $numRow mysql_num_rows($result);
        
        if (
    $numRow 0)
        {
            
    //Prend la valeur de retour    
            
    $Nom mysql_result($result,0);
        
            echo(
    $Nom'(#' .$varId.') is logged');
            
            
    //Enregistre le cookies!!!!!
            
    setcookie ("patriot_user_id" $varId time()+600);

        }
        else
        {
            echo(
    'Loggin failed');
        }    
    mysql_free_result($result);
    // closing the db link
    mysql_close ($link);


    ?>
    </body>
    </html>

  2. #2

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I want to add the cookie only after verifying if the ID is correct in my database, I've read that it need to be before the HTML tag but I do not understand where to put it. Anyone can help me to modify my code, I've loss 2 days to try to put it work and it doesn't, I really need you!!

    Daok

  3. #3

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    No one see my problem?

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Hey Daok,

    The setcookie() function MUST be called before any output is sent to the browser.

    Mean no HTML tags, echo statements, or blank spaces can come before you call the function.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Try doing something like this:

    Code:
    <?php
    // db variables
    // inserts variables accordingly
    $host = "myip";
    $user = "allo";
    $pass = "bye";
    $db = "nothere";
    $table = "boo";
    
        //Connection a la base de donnee
        $link = mysql_connect ($host, $user, $pass)
                or die("Impossible de se connecter : " . mysql_error());
        //Selection de la base utilisee        
         mysql_select_db($db);
        
        //Prend les parametres 
        $varId=$_POST['User_id'];
        
        //Prend le numéro de de l'équipe
        $sql = 'SELECT usagers_nom FROM usagers WHERE usagers_id = '  .$varId;
        
        //Execute l'ordre sql
        $result = mysql_query($sql)
            or die ("NO USER FOUND");
            
        $numRow = mysql_num_rows($result);
        
        if ($numRow > 0)
        {
            //Prend la valeur de retour    
            $Nom = mysql_result($result,0);
        
            //Enregistre le cookies!!!!!
            setcookie ("patriot_user_id" , $varId , time()+600);
    
    ?>
    <html>
    
    <head>
    <title>[   P   ] atriot</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <LINK href="patriot.css" rel="stylesheet" type="text/Css">
    </head>
    
    <body>
    <?php
    
            require_once("header.php");
            AfficherEntete();
    
            echo($Nom. '(#' .$varId.') is logged');
            
        }
        else
        {
            echo('Loggin failed');
        }    
    mysql_free_result($result);
    // closing the db link
    mysql_close ($link);
    
    
    ?>
    </body>
    </html>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    BIG BIG BIG THANKS to you man !

    Thx Hobo!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width