/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Can’t seem to set cookies.

Ok so I’ve got the entire registration system working, and the forgotten password. but for some reason I can’t set the cookies. When I look at the source after getting “Warning: Cannot modify header information – headers already sent” I notice that “</head><body>” is showing up twice for an unknown reason. I’ll just post the important code (skipping all the different action possible).

[code=php]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
<?
function login($user,$pass) {
$loginsql = “SELECT * FROM users WHERE email='”.$user.”‘”;
$loginresult = mysql_query($loginsql);

if ( mysql_num_rows($loginresult) == 0 ) {
return FALSE;
} else {
while ( $row = mysql_fetch_array( $loginresult ) ) {
if ( $user == $row[’email’] ) {
if ( $pass == $row[‘password’] ) {
$cookielength = 60*60*24*10;
setcookie(“Username”, $row[’email’], $cookielength);
setcookie(“login”, TRUE, $cookielength);
setcookie(“userid”, $row[‘idusers’], $cookielength);
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
}
}

if ( isset ( $_GET[action] ) ) {
$action = $_GET[action];
} else {
$action = ‘form’;
}
if ( $action == ‘submit’ ) {
if ( login( $_POST[’email’], $_POST[‘pass’] ) ) {
$content = “<h2>Thanks for logging in.</h2>”;
} else {
$content = “<h2>Login Failed!</h2>”;
}
} elseif ( $action == ‘form’ ) {
….
}

?>
</head><body>
<?
echo $content;
?>
</body>
</html>[/code]

By two closed head open body statements in the source i mean the error page is:

[quote]

Warning: Cannot modify header information – headers already sent by (output started at /home/rdenni5/public_html/family/login.php:6) in /home/rdenni5/public_html/family/login.php on line 28

Warning: Cannot modify header information – headers already sent by (output started at /home/rdenni5/public_html/family/login.php:6) in /home/rdenni5/public_html/family/login.php on line 29

Warning: Cannot modify header information – headers already sent by (output started at /home/rdenni5/public_html/family/login.php:6) in /home/rdenni5/public_html/family/login.php on line 30
Thanks for logging in.

[/quote]

and the source of that is:

[code=php]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
</head>
<body>

</head><body><h2>Login Failed!</h2>
</body>
</html>[/code]

Hope that is enough information for you. Sorry if its too much.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 19.2010 — The problem is that you are outputting text before your call to the login() function where the setcookie()'s are done. There can be no output -- including text or even white-space not within <?php...?> tags -- before any PHP function that sets HTTP headers, including setcookie(), session_start(), or header().

Probably the best solution is to reorganize the code so that anything that will set headers gets done before any output is generated, but as a work-around you could just do an ob_start() at the very beginning of the file (again before any output of any sort).
Copy linkTweet thisAlerts:
@DerokorianauthorOct 19.2010 — Thank you very much, reworked my code as you stated and everything is working fine now!

Much appreciated =D
×

Success!

Help @Derokorian spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...