0

I would like to set a cookie with an expiration time of 24 hours when a user enters Page A.

If the user visits Page B and he wasn't yet on Page A, he should stay on Page B.

However if the user visits Page B and the cookie from page A was already stored he should be automatically redirected to Page A.

Here is the PHP Code I inserted to the WP Plugin "My custom functions". Now my whole website ist just white and I can't do anything...

if( is_page('Page-A')){
    if(isset($_COOKIE['dz-offer'])){
        $cookie = $_COOKIE['dz-offer'];
    }
}
else{
    setcookie("dz-offer",time()+(3600*24));
    exit;
}

if( is_page('Page-B')){   
    if (!isset($_COOKIE['dz-cookie'])){
        header('Location: https://domain.com/page-a');
        exit;
    }
}
else{
   exit;
}

Any ideas how I can get that working? Thanks for your help!

+++ UPDATE +++

I deleted the Plugin "My custom functions" an now the white screen isn't there anymore... However if I reinstall the Plugin and activate it, the white space is there again before I can insert any code...

Lukas Lang
  • 21
  • 6
  • You have inconsistent cookie names. Some are called `dz-offer` others are called `cookie`. Anyway, there's no way for us to check your code. Please carefully read: http://stackoverflow.com/help/mcve – KIKO Software Aug 12 '16 at 13:35
  • Oh thanks! I just changed the cookie name. – Lukas Lang Aug 12 '16 at 13:42
  • Does it help to make it work? Also check all the `exit;`'s you use. If I read your code correctly it says: "If it is not page A then exit;". – KIKO Software Aug 12 '16 at 13:45
  • No unfortunately it doesn't work... there is still a fatal error. – Lukas Lang Aug 12 '16 at 13:56
  • Missing ) closing the if statements.. if( is_page('Page-A') ) { will be correct – Sebastian Krysiak Aug 12 '16 at 14:07
  • Thanks for your help, but it still doesn't work... – Lukas Lang Aug 12 '16 at 14:17
  • Ah there was still a ) missing after ('Page-B')... However know I can't acess my website anymore... It's just white... – Lukas Lang Aug 12 '16 at 14:20
  • You need to enable error display. Always when developing and testing PHP code, at the top of your script - `error_reporting(E_ALL); ini_set('display_errors', 1);`. What you're seeing is called the "white screen of death", because a fatal error or failed redirect is hiding. – Michael Berkowski Aug 12 '16 at 14:24
  • `setcookie()` and `header()` are sensitive to prior output and whitespace issues and the most common failures with them are described in [How to fix Headers already sent](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Michael Berkowski Aug 12 '16 at 14:25
  • This logic is spaghetti – Ryan Aug 12 '16 at 14:30
  • As I just said I'm not very familar with php coding... How would you do it? – Lukas Lang Aug 12 '16 at 14:32

0 Answers0