More Cookie Problems

Discussion in 'PHP' started by Handsofmodder, Nov 16, 2008.

  1. #1
    I keep on getting the following message whenever I try to create a cookie

    All I want to do is to create a cookie sucessfully whenever a user login into my test site (www.booo.vndv.com). Here's the code

    <?php
    function jack_in(){
    $user= $_POST['name'];
    setcookie('user',$user);
    print $_COOKIE['user'];
    }
    ?>
    <?php
    require "header.php";
    ?>
    <body>
    <?php
    function login_form($errors=''){
    if($errors){
    print 'Correct the following errors...';
    print '<li><ul>';
    print implode('</li><li>',$errors);
    print '</li></ul>';
    }
    print<<<_HTML_
    <form method="POST" action="$_SERVER[PHP_SELF]">
    Your Name:	<input type="text" name="name">
    <br>
    Your Password: <input type="password" name="password">
    <br>
    Done!: <input type="submit" value="done">
    <input type="hidden" name="_submit_check" value="1">
    </form>
    _HTML_;
    }
    
    function retrieve(){
    $errors= array();
    mysql_connect("localhost", "admin", "password");
    mysql_select_db("booo_db");
    $user_name = $_POST['name'];
    $password =$_POST['password'];
    $query = "SELECT user_password FROM users WHERE user_name='{$user_name}'";
    $result=mysql_query($query);
    $row=mysql_fetch_assoc($result); 
    if(!$password==$row['user_password']){
    $errors[]='Wrong User Name or Password';
    }
    return $errors;
    }
    
    if($_POST['_submit_check']){
    	if($form_errors=retrieve()){
    	login_form($form_errors);
    	}else{
    	jack_in();
    	}
    	}else{
    	login_form();
    	}
    ?>
    <?php
    require "footer.php";
    ?>
    PHP:

     
    Handsofmodder, Nov 16, 2008 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    You can't set cookie if output is started. Move the jack_in() call to the start of script.
     
    wmtips, Nov 16, 2008 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    just put session_start on top of the page.
     
    bartolay13, Nov 16, 2008 IP