ShaolinF Posted July 9, 2007 Share Posted July 9, 2007 Hi Guys, I have a html page and a php file. What I'm trying to do is once the user fills in the form on the html page and submits it, the php will set a cookie called visit. So when the user tries to visit the html page again, some code will look for the cookie and redirect to the appropriate page. I've done half of that. When the user submits a cookie is created, its just when it comes to checking the second time round for the html page that I get an error: Warning: Cannot modify header information - headers already sent by (output started at /home/***/public_html/details.php:1) in /home/shaolin/public_html/check.php on line 6 Here is th code for check.php: <?php if (isset($_COOKIE['visit'])) { header('Location: http://www.redirect.com/inbox/'); } elseif (empty($_COOKIE[visit])) { echo 'This is your first time visiting this page.'; } ?> Here is the code to create the cookie: <?php $value = 'visit'; setcookie("visit", $value, time()+43200); /* expire in 12 hours */ if (isset($_COOKIE['visit'])) { header('Location: http://www.facebook.com/inbox/'); } elseif (empty($_COOKIE[visit])) { echo 'This is your first time visiting this page.'; } ?> Quote Link to comment Share on other sites More sharing options...
aim25 Posted July 9, 2007 Share Posted July 9, 2007 Put the php code that checks for the cookie before anything else on the page. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 Ah, Love this place, instant replies. Here you go: <?php if (isset($_COOKIE['visit'])) { header('Location: http://www.redirect.com/inbox/'); } elseif (empty($_COOKIE[visit])) { echo 'This is your first time visiting this page.'; } ?> Quote Link to comment Share on other sites More sharing options...
aim25 Posted July 9, 2007 Share Posted July 9, 2007 And change elseif (empty($_COOKIE[visit])) to elseif (empty($_COOKIE['visit'])) Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 Changed it but I'm still getting an error when I view the html page for the second time. Error: Warning: Cannot modify header information - headers already sent by (output started at /home/****/public_html/details.php:1) in /home/shaolin/public_html/check.php on line 6 Line 6 is: header( 'Location: http://www.yoursite.com/new_page.html' ) ; Quote Link to comment Share on other sites More sharing options...
aim25 Posted July 9, 2007 Share Posted July 9, 2007 Well i guess this may be it http://www.geeklog.net/faqman/index.php?op=view&t=38 Quote Link to comment Share on other sites More sharing options...
marcus Posted July 9, 2007 Share Posted July 9, 2007 Your elseif statement on your second code snippet it useless, you're setting the cookie, then checking to see whether it exists or doesn't exist. From my perspective, it exists, so why bother checking that it DOESN'T exist? And if you're in a hurry just use ob handling. <?php ob_start(); //php stuff ob_end_flush(); ?> Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 Aim25: I removed all whitespace to no avail. mgallforever: The point of that code is to check whether the cookie exists. The cookie is only added once the form is submitted. Hence, after the form is submitted and the user wants to view the form again, he will be redirected. Warning: Cannot modify header information - headers already sent by (output started at /home/****/public_html/details.php:1) in /home/shaolin/public_html/check.php on line 4 Here is the form setup: <? include("check.php");?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <head> </head> etc etc Quote Link to comment Share on other sites More sharing options...
marcus Posted July 9, 2007 Share Posted July 9, 2007 Well if you're creating the cookie by form, shouldn't you check if it's been made first, then create the cookie? $submit = $_POST['submit']; if($submit){ if($_COOKIE['name']){ //go somewheres }else { //make cookie } } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 9, 2007 Share Posted July 9, 2007 The output is starting on line 1 in details.php. The error is being flaged up on line 6 in check.php. details.php is the problem file not check.php. Check details.php that there is no output on line 1. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 Ok, basically I have 2 php files. One is a form (Lets call it F1) and the other is a php scipt which will post the details to me and add the cookie (lets call it P2). Now, when the user visits F1, a script checks whether a cookie exists, if the cookie exists the user is redirected. If it doesnt exist then the page stays the same. Once the user has filled in the form and submitted it, P2 will email me the form and make a cookie. So if the user tries to go onto F1 a second time he will be redirected because he now has a cookie. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 The output is starting on line 1 in details.php. The error is being flaged up on line 6 in check.php. details.php is the problem file not check.php. Check details.php that there is no output on line 1. Thanks, this is line 1 on details.php: <? include("check.php"); ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 The output is starting on line 1 in details.php. The error is being flaged up on line 6 in check.php. details.php is the problem file not check.php. Check details.php that there is no output on line 1. Thanks, this is line 1 on details.php: <? include("check.php"); ?> Try <?php include("check.php"); ?> Also, if you are using dreamweaver, sometimes they do weird stuff. Copy the code for details.php and paste it in notepad, make sure that the <?php is at the VERY top, in both details and check.php either or will throw the error. Submit to the server and see if that fixes it. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 I went as far as removing all the code from details.php, only leaving <?php include("check.php"); ?> and it still didn't work .... Quote Link to comment Share on other sites More sharing options...
marcus Posted July 9, 2007 Share Posted July 9, 2007 What you've shown us, is that all of check.php's file contents, or just a snippet? Quote Link to comment Share on other sites More sharing options...
aim25 Posted July 9, 2007 Share Posted July 9, 2007 Post up all your code. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 All of it. Here it is again: <?php if (isset($_COOKIE['visit'])) { $URL="http://www.example.com"; header ('Location: $URL'); } elseif (empty($_COOKIE['visit'])) { echo 'This is your first time visiting this page.'; } ?> The problem is with header ('Location: $URL'); I've copied all of check.php content into details.php and it gives me the same error. Quote Link to comment Share on other sites More sharing options...
marcus Posted July 9, 2007 Share Posted July 9, 2007 Since your cookie has a value I suggest checking to see if the cookie has the value you set it to. So just use ONE if statement to see if the cookie equals that, then if it does, it's there first time, or they edited the cookie. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 Since your cookie has a value I suggest checking to see if the cookie has the value you set it to. So just use ONE if statement to see if the cookie equals that, then if it does, it's there first time, or they edited the cookie. I dont quite understand what you mean. I deleted all my cookies/temp net files and re-ran through the process but i couldnt find the actual cookie in the temp net files dir.. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 If anyone has a script a create to a cookie, and a script to read a cookie and follow through with the approapriate action, please do share. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 I went as far as removing all the code from details.php, only leaving <?php include("check.php"); ?> and it still didn't work .... What editor are you using to modify the file? Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 notepad++ Quote Link to comment Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 Alright here are just some basic checks. Make sure the file is not being manipulated after uploaded to the server (view the file on the server and check for any type of space, enter gap whatever before the first <?php statement) If there is it is a problem with how your ftp program uploads the file, what FTP program are you using? Also check the type of file you are working with in Notepad++ Make sure it is not the UNIX one (I believe that one is known for this type of issue) Other than that I do not know what to have you try. The issue is definitely with the file on the server having an extra space at the top. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted July 9, 2007 Author Share Posted July 9, 2007 Well, the problem was with the header redirect. I got tired of trying to fix it, so I used the include instead and redirected via that method. Works now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.