ThemeShaper Forums » Thematic

[closed]

Cookie, Session: add_action to wp_head not correct

(10 posts)
  • Started 5 years ago by elderberry
  • Latest reply from helgatheviking
  • This topic is not resolved
  1. elderberry
    Member

    Hi All,

    I have a little php script that allows a site visitor to leave the site and temporarily disables the ability to return to the site. I added this to functions.php and the cookie is getting set, but the new header is not being sent. Can I add this to head.php or header-extensions.php, and if so where (that file scares me :-)?

    function emergency_script() {
    
    if ($_SESSION['emergency'] == 'true') {
    
        header('Location: http://familyrefugecenter.org/scripts/emergency.php');
        exit;
    }
     }
    add_action('wp_head','emergency_script');
    Posted 5 years ago #
  2. wp_head is probably too late to change the location? just guessing. try adding your script to some other earlier hooks. template_redirect maybe even init

    Posted 5 years ago #
  3. elderberry
    Member

    Thanks Helga - neither worked though :-( Is init the earliest hook?

    Posted 5 years ago #
  4. elderberry
    Member

    Helga, thanks so much for your time and help. I am still having no success - here's my latest effore-

    function emergency_redirect() {
    if ($_SESSION['emergency'] == 'true') {
    
    wp_redirect( 'http://familyrefugecenter.org/scripts/emergency.php', 302);
    exit;
    }
    }
    add_action('init','emergency_redirect');
    Posted 5 years ago #
  5. try doing a var_dump($_SESSION) or i usually like print_r($_SESSION) to see what is being set for the emergency key. are you setting the key as a boolean true/false, or a string "true" / "false" ?

    Posted 5 years ago #
  6. I'm wondering two things where are you calling session_start() and how are you expiring the session?

    Posted 5 years ago #
  7. elderberry
    Member

    Here's the original information I got from the person who wrote the script.

    My system relies on a bit of server-side scripting, specifically PHP. Here's how it works:

    The emergency exit button links to a PHP script on the server, emergency.php. The script creates a browser cookie that stores an variable called "emergency" and sets it to "true." Then the script loads the RSS news feed from Google News, chooses a random link, and redirects the browser to it.
    Every single page on the site has a PHP include that checks for the emergency cookie. If it is set to true (that is, if someone has clicked the emergency exit button recently), it redirects to the emergency.php script, which then redirects to a random link again.
    If someone clicks back from the random link, or even if they manually try to visit the site by entering it in the address bar, the site will again check for the cookie and go to another site when it sees the emergency cookie.
    The browser history is not manipulated in any way. Technically, the name of the site will be listed in the history, but attempts to visit the site by clicking on it will still be hindered.

    Here is what the emergency.php code looks like:

    <?php
    
    function load_xml_feed($location)
    {
    global $value1;
    $feedXml = simplexml_load_file($location);
    
    $i= '1';
    foreach ($feedXml->channel->item as $article)
    {
    $title[$i] = (string)$article->title;
    $link[$i] = (string)$article->link;
    $description[$i] = (string)$article->description;
    
    $i++;
    
    }
    $randnumber = rand(2, $i);
    $link = trim($link[$randnumber]);
    $title = trim($title[$randnumber]);
    $description = trim($description[$randnumber]);
    $title = iconv("UTF-8", "ISO-8859-1", $title);
    $description = iconv("UTF-8", "ISO-8859-1", $description);
    $value1 = array($title,$link,$description);
    return $value1;
    }
    
    $rss = 'http://news.google.com/news?ned=us&topic=h&output=rss';
    load_xml_feed($rss);
    $link = $value1[1];
    $title = $value1[0];
    $description = $value1[2];
    
    if ($link == '') { $link = 'http://www.google.com/search?hl=en&q=weather+forecast&btnG=Google+Search&aq=f&oq='; }
    
    session_start();
    $_SESSION['emergency'] = 'true';
    
    header("Location: " . $link);
    exit;
    
    ?>
    
    And here is what the include at the top of every page looks like:
    session_start();
    
    if ($_SESSION['emergency'] == 'true') {
    
        header('Location: emergency.php');
        exit;
    
    }
    Posted 5 years ago #
  8. Not necessarily the only way but this is how I'd start:

    function my_timed_session() {
    	session_start();
    
    	if ( isset ( $_GET['emergency'] ) ) {
    		$_SESSION['emergency'] = time();
    	}
    
    	if ( isset ($_SESSION['emergency'] ) && ( time() - $_SESSION['emergency'] < 10 ) ) {
    		wp_redirect( my_random_link_generator() , 302 );
    		exit;
    	}
    }
    add_action( 'init', 'my_timed_session', 1 );
    
    function my_random_link_generator() {
    	// do something to randomly generate link
    	return $link
    }

    Then your link to escape the site and redirect return visits for the specified time... could be http://example.com?emergency=1

    Posted 5 years ago #
  9. @elderberry - i'm in over my head on this one, but fyi- please put code between backticks and not apostrophes (since they don't do anything)

    Posted 5 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.