Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Load site based on cookie value in PHP

Post

Load site based on cookie value in PHP

+3
−0

I need to load a site based on a cookie. I wrote code to validate that, like this.

if(!isset($_COOKIE['cookie'])){
    $domain = $_SERVER['SERVER_NAME'];
    setcookie('cookie', $cookie, time() + 31536000, '/', $domain);
    header('Location: '.$_SERVER['REQUEST_URI']);   // reload
}

But when the cookie was created the first time I got this error:

This page isn't working - redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Checking "too early" for cookie (4 comments)
Checking "too early" for cookie
Alexei‭ wrote about 2 years ago

I have not programmed in PHP for ages, but looking at the docs it seems that "Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array." Your code seems to immediately redirect and the cookies array might not contain the just set cookie.

More important: what are you trying to achieve? Why do you have the need to reload the page?

hajakutbudeen‭ wrote about 2 years ago

Thanks for your reply Alexei. we have products from UK and products from US we need to load products and other site content based on cookie value, cookie value will be detect based on IP Geo. so for that i need to check cookie is set or not before page load. as per my code i checked if there's no cookie create new one. after create cookie i need to reload that page so that it will detect. cookie and load product and content based on that.

Right now what happen when i use header('Location: '.$_SERVER['REQUEST_URI']); it stop page loading i guess / loop many times not sure. so it shows error message. After 2 sec it redirect to REQUEST_URI, it's happen when ever cookie created first time.

Alexei‭ wrote about 2 years ago

I am wondering if it works if you change the server-side "reload" to a client-side one: location.reload()

Zakk‭ wrote almost 2 years ago · edited almost 2 years ago

I don't know if this will solve your issue, but I think it's better to call exit() after header():

header('Location: '.$_SERVER['REQUEST_URI']);   // reload
exit();

You can make it as a reusable function if you wish:

function goto_page(string $page): void
{
    header('Location: ' . $page);
    exit();
}

and call it like: goto_page($_SERVER['REQUEST_URI']);.