Redirect if cookie isn't set does not work

I use cookies on my websites and these cookies should be deleted if an user isn't on a specific page. This works perfectly but the problem is that if a user is on the page and goes to another page which is a child of some page and then they try to go back it automatically redirects them to the 'Products' page, which. If the user goes to any page that is not a child page and then goes back, it doesn't redirect them.

Code:

if (isset($_POST['product']) && is_page('objednat')) {     setcookie('order', $_POST['product']);     redirect('objednat'); } elseif (!isset($_COOKIE['order']) && is_page('objednat')) {     redirect('vyklady'); } elseif (isset($_COOKIE['order']) && is_page('objednat')) {     $order = create_order($_COOKIE['order']); } elseif (isset($_COOKIE['order']) && !is_page('objednat')) {     setcookie('order', '', time() - 3600);     setcookie('order-data', '', time() - 3600);     redirect(get_the_id()); }  

Redirect function code:

function redirect($page = FALSE) {     (!is_numeric($page)) ? wp_redirect($page) : wp_redirect(get_permalink($page)); } 

EDIT:

I hope this is a better way to explain the problem. I just want to redirect the person to another page if the cookie does not exist. I have 3 main pages and the third page contains the child pages. If I click on one of the child pages, the cookie gets deleted and everything works fine, but if I go to one of the main pages, the cookie also deletes but I can go back to the 'Order page' and it doesn't redirect me.

EDIT 2:

Solved, I forgot to specify the cookie path. Originaly it was the child pages path. After setting it for the whole domain it works.

Replay

Category: php Time: 2016-07-28 Views: 1

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.117 (s). 12 q(s)