Jump to content

setcookie problem


Sniffy

Recommended Posts

On my site, I'm going to make a log in system, but right now you just set a cookie when you log in, but I'm causing errors. Can you help me?Here is my site.http://bobville.9999mb.com/test.phpbobville is the password to the log in.Here's the code for test.php

<html><head><link rel='stylesheet' type='text/css' href='style.css'><title>!!Bobville Online!!</title><script type='text/javascript'>function addAdmin(adName){document.getElementById("update").innerHTML += "Posted By "+adName+".";}function sigWeek(theSig){document.getElementById("sigDisplay").src ="images/sotw/"+theSig+".gif";}</script></head><body onload='addAdmin("Jud")'><object width='980' height='150'><param name='banner'><embed src='banner.swf' width='980' height='150'></embed></object><br /><table cellspacing ='0'><tr><td width='210'><?phpinclude("menu.php");?></td><td width='580'><table cellspacing='0'><tr><td width='140'><input type='button' id='normalButton' value='SOTW #1' name='SOTW1' onclick='sigWeek(1)'><br /><input type='button' id='normalButton' value='SOTW #2' name='SOTW2' onclick='sigWeek(2)'><br /><input type='button' id='normalButton' value='SOTW #3' name='SOTW3' onclick='sigWeek(3)'><br /></td><td width='300'><img src='images/sotw/1.gif' id='sigDisplay' width='300' height='100'></td><td width='140'><input type='button' id='normalButton' value='SOTW #4' name='SOTW4' onclick='sigWeek(4)'><br /><input type='button' id='normalButton' value='SOTW #5' name='SOTW5' onclick='sigWeek(5)'><br /><input type='button' id='normalButton' value='SOTW #6' name='SOTW6' onclick='sigWeek(6)'><br /></td></tr></table><br /><div class='maintitle' align='center'><div class='valign'>Update #1</div></div><br /><p id='update'>Hello, and welcome to the Bobville Homepage.<br />Currently, it's still under construction and we need some time to build it.<br />Check back later.<br /></p></td><td width='210'><?php if(isset($_COOKIE['user'])){echo "Welcome back,".$_COOKIE['user']."<br /><div class='menu'><a href='accountinfo.php'>Account Info</a><br /><a href='updateprofile.php'>Update Profile</a></div><br />";}else{echo "<form method='post' action='login.php'>Username:<br /><input type='text' id='uname' name='uname' value='uname'><br />Password:<br /><input type='password' id='pword' name='pword' value='pword'><br /><input type='submit' id='normalButton' onSubmit='checkForm()' value='Log in'>";}?></form></td></tr></table></body></html>

And here is the code for login.php which the form directs to.

<html><head><link rel='stylesheet' type='text/css' href='style.css'><title>!!Bobville Online!!</title><script type='text/javascript'>function addAdmin(adName){document.getElementById("update").innerHTML += "Posted By "+adName+".";}function sigWeek(theSig){document.getElementById("sigDisplay").src ="images/sotw/"+theSig+".gif";}</script></head><body><object width='980' height='150'><param name='banner'><embed src='banner.swf' width='980' height='150'></embed></object><br /><table cellspacing ='0'><tr><td width='210'><?phpinclude("menu.php");?></td><td width='580'><table cellspacing='0'><tr><td width='140'><input type='button' id='normalButton' value='SOTW #1' name='SOTW1' onclick='sigWeek(1)'><br /><input type='button' id='normalButton' value='SOTW #2' name='SOTW2' onclick='sigWeek(2)'><br /><input type='button' id='normalButton' value='SOTW #3' name='SOTW3' onclick='sigWeek(3)'><br /></td><td width='300'><img src='images/sotw/1.gif' id='sigDisplay' width='300' height='100'></td><td width='140'><input type='button' id='normalButton' value='SOTW #4' name='SOTW4' onclick='sigWeek(4)'><br /><input type='button' id='normalButton' value='SOTW #5' name='SOTW5' onclick='sigWeek(5)'><br /><input type='button' id='normalButton' value='SOTW #6' name='SOTW6' onclick='sigWeek(6)'><br /></td></tr></table><br /><div class='maintitle' align='center'><div class='valign'>Logging In</div></div><br /><p id='update'><?php$uname = $_POST['uname'];$pword = $_POST['pword'];if(isset($_COOKIE['user']) && $pword == "bobville"){echo "You are already logging in.<br /><a href='index.php'>Return to the Main Page</a>";}else if($pword == "bobville" && !isset($_COOKIE['user'])){setcookie("user", $uname, time()+360000);}else{echo "Your password is incorrect, please try again.";}?></p></td><td width='210'><?php if(isset($_COOKIE['user'])){echo "Welcome back,".$_COOKIE['user']."<br /><div class='menu'><a href='accountinfo.php'>Account Info</a><br /><a href='updateprofile.php'>Update Profile</a></div><br />";}?></form></td></tr></table></body></html>

And here is the error that displays on my login.php if you get the correct password and set the cookie.

Warning: Cannot modify header information - headers already sent by (output started at /www/9999mb.com/b/o/b/bobville/htdocs/login.php:7) in /www/9999mb.com/b/o/b/bobville/htdocs/login.php on line 60

What is this problem and how do I fix this?

Link to comment
Share on other sites

Cookies need to be set and handled in the HEADERS section of the html page. Simply said, this is an exchange between the client (the user's web browser) and the server (your server where the page is hosted) that happens before ANY of the HTML content is sent to the client.Meaning, if you already start sending content to the client (including <html>, <head>, and whitespace) then it's too late to set cookies. If you echo() anything, or close off php parsing and output HTML, then it's too late to set cookies.Here's an example. I just took the code that included setcookie() and moved it before the data. It saves the intended text as a variable $TEXT, which is output later in the page.

// START OF FILE<?php$uname = $_POST['uname'];$pword = $_POST['pword'];if(isset($_COOKIE['user']) && $pword == "bobville"){  $TEXT = "You are already logging in.<br /><a href='index.php'>Return to the Main Page</a>";}else if($pword == "bobville" && !isset($_COOKIE['user'])){  setcookie("user", $uname, time()+360000);}else{  $TEXT = "Your password is incorrect, please try again.";}?><html><head>// YOUR HEADER INFO</head><body>// SOME HTML<div class='maintitle' align='center'>  <div class='valign'>Logging In</div></div><br /><p id='update'><?php  echo $TEXT;?></p>//THE REST OF YOUR STUFF

Link to comment
Share on other sites

Wait, I had another error. It worked for awhile and then something happened, another parse error.Here's login.php

<?php$uname = $_POST['uname'];$pword = $_POST['pword'];if(isset($_COOKIE['user']) && $pword == "bobville"){$loginText = "You are already logging in.<br /><a href='index.php'>Return to the Main Page.</a>";}else if($pword == "bobville" && !isset($_COOKIE['user'])){setcookie("user", $uname, time()+360000);$loginText = "You've been successfully logged in, ".$_COOKIE['user'].", <a href='index.php'>Return to the Main Page.</a>";}else{$loginText = "Your password is incorrect, please try again.";}?><?phpif(isset($_COOKIE['user'])){$loginDisplay = "Welcome back,".$_COOKIE['user']."<br /><div class='menu'><a href='accountinfo.php'>Account Info</a><br /><a href='updateprofile.php'>Update Profile</a></div><br />";}else{$loginDisplay = "You are not logged in, <br />Please go to the home page.";?><html><head><link rel='stylesheet' type='text/css' href='style.css'><title>!!Bobville Online!!</title><script type='text/javascript'>function addAdmin(adName){document.getElementById("update").innerHTML += "Posted By "+adName+".";}function sigWeek(theSig){document.getElementById("sigDisplay").src ="images/sotw/"+theSig+".gif";}</script></head><body><object width='980' height='150'><param name='banner'><embed src='banner.swf' width='980' height='150'></embed></object><br /><table cellspacing ='0'><tr><td width='210'><?phpinclude("menu.php");?></td><td width='580'><?phpinclude("sotw_content.php");?><br /><div class='maintitle' align='center'><div class='valign'>Logging In</div></div><br /><p id='update'><?phpecho $loginText;?></p></td><td width='210'><?php echo $loginDisplay;?></form></td></tr></table></body></html>

Here's the error.

Parse error: parse error, unexpected $ in /www/9999mb.com/b/o/b/bobville/htdocs/login.php on line 80

Link to comment
Share on other sites

That also means it has come to an unexpected end. So, in other words you need to find some logic you didnt close (if, while, for, etc..). I took my ten seconds of time and found it for you:

<?php$uname = $_POST['uname'];$pword = $_POST['pword'];if(isset($_COOKIE['user']) && $pword == "bobville"){$loginText = "You are already logging in.<br /><a href='index.php'>Return to the Main Page.</a>";}else if($pword == "bobville" && !isset($_COOKIE['user'])){setcookie("user", $uname, time()+360000);$loginText = "You've been successfully logged in, ".$_COOKIE['user'].", <a href='index.php'>Return to the Main Page.</a>";}else{$loginText = "Your password is incorrect, please try again.";}?><?phpif(isset($_COOKIE['user'])){$loginDisplay = "Welcome back,".$_COOKIE['user']."<br /><div class='menu'><a href='accountinfo.php'>Account Info</a><br /><a href='updateprofile.php'>Update Profile</a></div><br />";}else{$loginDisplay = "You are not logged in, <br />Please go to the home page.";}?><html><head><link rel='stylesheet' type='text/css' href='style.css'><title>!!Bobville Online!!</title><script type='text/javascript'>function addAdmin(adName){document.getElementById("update").innerHTML += "Posted By "+adName+".";}function sigWeek(theSig){document.getElementById("sigDisplay").src ="images/sotw/"+theSig+".gif";}</script></head><body><object width='980' height='150'><param name='banner'><embed src='banner.swf' width='980' height='150'></embed></object><br /><table cellspacing ='0'><tr><td width='210'><?phpinclude("menu.php");?></td><td width='580'><?phpinclude("sotw_content.php");?><br /><div class='maintitle' align='center'><div class='valign'>Logging In</div></div><br /><p id='update'><?phpecho $loginText;?></p></td><td width='210'><?phpecho $loginDisplay;?></form></td></tr></table></body></html>

You just didn't end the if/else that checked if the user was logged in :). I dont know if I ended it in the right spot though.. you need to fix that.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...