1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Cookie Problems

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

  1. #1
    Hi! I almost finished the login system for my Rickrolla website. But, I keep having problems with the Cookies. This error keeps occuring

    Here's the code for login.php
    <?php
    require "header.php";
    ?>
    <body>
    <?php
    function login($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['password']){
    $errors[]="Wrong username or password";
    }
    }
    if($_POST['_submit_check']){
    	if($form_errors=retrieve()){
    	login($form_errors);
    	}else{
    	doit();
    	}
    }
    
    if(!$_POST['_submit_check']){
    login();
    }
    ?>
    <?php
    require "footer.php";
    ?>
    PHP:
    Here's the code for header.php
    <?php
    function doit(){
    setcookie($row['user_name']);
    print "You're login Mr.".$_COOKIE['user_name'];
    }
    ?>
    <html>
    <head>
    <title>Rickroller.com</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <div id="Header">
    	<div id="Nav" align="right">
    		<a href="register.php">Register Here!</a>/ 
    		<?php
    		if(empty($me)){
    		print '<a href="login.php">Login Here!</a>/';
    		}else{
    		print '<a href="user.php?user='.$me.'">My Homepage </a>/';
    		}
    		?>
    		</div>
    </div> 
    	
    PHP:

     
    Handsofmodder, Nov 12, 2008 IP
  2. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    the main issue that causes this error is whit space at the top and bottom of php files....

    remeber to remove and space before the first and after the last php tags
     
    cornetofreak, Nov 12, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Do u notice you actually output something before u call the doit() function.

    functions like setcookie() , and header() wouldn't allow any output before it (including blank spaces).

    replace your header.php (your most top file) with this to your header.php

    
    <?php
    ob_start();
    function doit(){
    setcookie($row['user_name']);
    print "You're login Mr.".$_COOKIE['user_name'];
    }
    ?>
    
    PHP:
     
    ads2help, Nov 12, 2008 IP