Forums

[resolved] Help! Trying to add php session_start() to header (6 posts)

  1. Heerefishyfish
    Member
    Posted 1 year ago #

    Hi all,

    I'm trying to implement some age verification script for a beer-related site. However, when I add the script to create an array before anything else in the header, I get an error, saying the header has already been sent...

    Here is the code I'm using:

    <?php
    session_start();
    $unprotected_pages = array('/ageGate.php', '/terms.php', '/underage.php');
    if ((!in_array($_SERVER['PHP_SELF'], $unprotected_pages)) && (empty($_SESSION['age_verified']))) {
        header("Location: http://{$_SERVER['SERVER_NAME']}/ageGate.php");
        exit;
    }
    ?>

    Any thoughts on how I could make this work?

    Thanks!
    HFF

  2. iridiax
    Member
    Posted 1 year ago #

  3. Heerefishyfish
    Member
    Posted 1 year ago #

    Thanks- I'll check it out.

  4. Heerefishyfish
    Member
    Posted 1 year ago #

    Alright I looked into that link... I don't see any white space or any junk in my code...I wonder if it's another issue. This is the error message I've been receiving:
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/50/6213350/html/wp-content/themes/miller/splash-page.php:6) in /home/content/50/6213350/html/wp-content/themes/miller/header-splash.php on line 2

    So it seems like based on my code, it's not accepting the session_start(); for some odd reason. Here is my code again for reference:

    <?php
    session_start();
    $unprotected_pages = array('/ageGate.php', '/terms.php', '/underage.php');
    if ((!in_array($_SERVER['PHP_SELF'], $unprotected_pages)) && (empty($_SESSION['age_verified']))) {
        header("Location: http://{$_SERVER['SERVER_NAME']}/ageGate.php");
        exit;
    }
    ?>
  5. iridiax
    Member
    Posted 1 year ago #

    I don't know if there is anything wrong with the session code, but I do know that WordPress itself doesn't use sessions.

    http://codex.wordpress.org/WordPress_Cookies

    The issue may also be your splash page, as these are not simple to set up with WordPress.

    http://codex.wordpress.org/User:PotterSys/Home_Page_as_a_Splash_Page

  6. Heerefishyfish
    Member
    Posted 1 year ago #

    Thanks Iridiax. I actually did some more digging in google, and found a small fix for this problem. Basically I had to add the session_start() code to the first part of the wp-config.php file.

    So that sorts that out!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.