SET_COOKIE function *Unsolved

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darksevear
Forum Newbie
Posts: 11
Joined: Sat May 13, 2006 10:38 pm

SET_COOKIE function *Unsolved

Post by Darksevear »

Just wondering what context can the Set_Cookie function be put into. Cause i have tried to use it in normal php code but it just wont seem to work. Do i have to use it in the header? Or outside HTML tags? I have seen that in some examples.

EDIT: I am trying to get information from a form then put it in a cookie (Form is on same page) How do i write this?

Is it like

Code: Select all

Set_Cookie($var)

<html>

<form>

set_cookie($var)

</html>
Please Advise

(btw, this forum is great)
Last edited by Darksevear on Mon May 15, 2006 6:08 am, edited 2 times in total.
Darksevear
Forum Newbie
Posts: 11
Joined: Sat May 13, 2006 10:38 pm

Post by Darksevear »

Please help im desperate and a total newbie
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

:arrow: http://uk.php.net/setcookie :)

you have to do it before anything has been put on the page.

Code: Select all

<?php

if (isset($_POST['submit'])) {  // check to see if the forum has been submitted
    //  where is tasys 'submit', use the name of the submit button on the form
    $cookieString = "data string to set";  \\ Data
    setCookie ("name of the cookie", $cookieString, "time to expire");  // set the cookie
}

?> 

<html>
   blah blah blah
</html>
Darksevear
Forum Newbie
Posts: 11
Joined: Sat May 13, 2006 10:38 pm

Post by Darksevear »

Thanks, so much.

Cheers, Daniel

But, This wont work:

Code: Select all

<?php

if (isset($_POST['submit'])) {  // check to see if the forum has been submitted
    //  where is tasys 'submit', use the name of the submit button on the form 
	include('connect.php');


	// get form input
    // check to make sure it's all there
    // escape input values for greater safety
    $user = empty($_POST['user']) ? die ("ERROR: Enter a Username") : mysql_escape_string($_POST['user']);
    $pass = empty($_POST['pass']) ? die ("ERROR: Enter a Password") : mysql_escape_string($_POST['pass']); 

	$sql="SELECT * FROM users WHERE user='$user' and pass='$pass'";
	$result=mysql_query($sql);

	// Mysql_num_row is counting table ro

	$count=mysql_num_rows($result);

	// If result matched $user and $pass, table row must be 1 row

	if($count==1){

		// Register $user, $pass and redirect to file "login_success.php" and make cookie to save user data
                echo "<meta http-equiv='refresh' content='0;url=login_sucess.php'>";
		setcookie('user', $user, time()+36000*24*365);

	}
	else {
		echo "Wrong Username or Password";
	}
}

?> 

<html>
<head>
</head>

<body>

<?php
if (!isset($_POST['submit'])) {
// form not submitted
?>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    Username: <input type="text" name="user">
    Password: <input type="text" name="pass">
    <input type="submit" name="submit">
    </form>

<?php
}
?>
</body>
</html>
It keeps giving me the message:
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\website\connect.php:9) in C:\Program Files\xampp\htdocs\website\login.php on line 27
I dont know whats wrong. Its when I fill in the form and try to submit.

Thanks, Daniel[/quote]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

When you don't know what's going on, the first thing to do is search the web... I'm quite sure that you're not the first with this problem and a websearch for 'headers already sent ' will return _many_ topics with an answer for your problem.
Post Reply