close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

If cookie is found, page is not redirecting

Thread began 3/11/2011 9:15 am by sandy170299 | Last modified 3/11/2011 12:04 pm by Jason Byrnes | 3754 views | 7 replies |

sandy170299

If cookie is found, page is not redirecting

On index2.php, I am creating a cookie using the following code:
<?php
if ("" == "") {
setcookie("ipaddress", "".((isset($_SERVER["REMOTE_ADDR"]))?$_SERVER["REMOTE_ADDR"]:"") ."", time()+(60*60*24*30), "/", "", 0);
}
?>

On the main page, index.php, I have the following code:
<?php echo((isset($_COOKIE["ipaddress"]))?$_COOKIE["ipaddress"]:"") ?>
<?php // find the ip address cookie, if its set, redirect
if (isset($_cookie["ipaddress"])) {
// redirect
header ('Location: index2.php');
}
?>

The problem is, it's not redirecting. I've output the cookie on the page, so I know it's there, but I can't figure out why it's not redirecting to index2.php once it's found?

Thanks!

Sign in to reply to this post

Jason ByrnesWebAssist

the problem is that you have the echo before the header function which will cause the headers already sent error.


your if statement must be before any output on the page, if you try to call the header function after there has been any output to the page, it will fail.


see this post for more details on the headers already sent error:
showthread.php?p=92580#post92580

I know you are not reporting the headers already sent error, but the echo statement before the header function is causing it to occur, and that is the root of the problem.

Sign in to reply to this post

sandy170299

I removed this line

<?php echo((isset($_COOKIE["ipaddress"]))?$_COOKIE["ipaddress"]:"") ?>

from line 1, but it's still not redirecting. I also was not receiving the "headers already sent" message. There were no errors at all. I only added that statement after it wasn't redirecting - to make sure that it was actually capturing the IP address.

Sign in to reply to this post

Jason ByrnesWebAssist

you may not have error reporting on to see the error message.

add the following at line 1:

php:
<?php

error_reporting
(E_ALL);
ini_set('display_errors','on');
?>




to turn error reporting on.

To troubleshoot any further, i would need a copy of the page.

Sign in to reply to this post

sandy170299

Sorry, Jason. I'm still not getting any errors at all. I will zip the two pages for you and attach them.

Attached Files
WA_ERRORS.zip
Sign in to reply to this post

Jason ByrnesWebAssist

keep in mind that php is a case sensitive language.

there is no variable in php names;
$_cookie

to reference cookies, sessions, form post or URLS, you must use capitols:
$_COOKIE


your echo line had the correct case:
<?php echo((isset($_COOKIE["ipaddress"]))?$_COOKIE["ipaddress"]:"") ?>


but your if statement does not:
if (isset($_cookie["ipaddress"])) {


it should be:
if (isset($_COOKIE["ipaddress"])) {

Sign in to reply to this post

sandy170299

Bingo! That's what I get for getting used to ColdFusion! You can pretty much do anything with it and put anything anywhere it doesn't care :P Thank you, Jason :)

Sign in to reply to this post

Jason ByrnesWebAssist

yeah, php is a bit more strict in terms of syntax, but i find it a lot more flexible than cold fusion.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...