Jump to content

Cookies


ShaolinF

Recommended Posts

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.'; 
}
?>

Link to comment
Share on other sites

Ah, Love this place, instant replies.  ;D

 

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.'; 
}
?>

Link to comment
Share on other sites

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' ) ;

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
  }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"); ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.