0

I have a main page including a header page to make editing the header across all pages a quicker process. The issue I am running into is that this header has search option on it, and I want the search term to be saved in a session variable. It works fine if the header is on the page itself and not on an included page, but as soon as I put the header into its own file the search function stops working as intended.

this is simply the include code:

<?php 
if (!isset($_SESSION)) {
  session_start();
}

$redirectAction = $_SERVER['PHP_SELF'];
?>
<html>
  <body>
    <?php include 'http://www.saxon564.com/tclusa/header.php'; ?>
  </body>
</html>

Here is the header.php code:

    <?php
    if (!isset($_SESSION)) {
      session_start();
    }


    if ((isset($_POST["search"])) && ($_POST["search"] == "1")) {

        $_SESSION['query'] = $_POST['query'];
    }
    ?>
    <html>
      <body>
          <form action="<?php echo $redirectAction; ?>" method="POST">
                  <input type="text" name="query" id="query" placeholder="Search" size="20" value="<?php echo $_SESSION['query']; ?>" />
                  <input type="hidden" name="search" id="search" value="1" />
                  <input type="submit" class="sbmt" value="Search" />
          </form>
      </body>
    </html>

What happens is when I submit the search, the page reloads, but the search data was not saved. I am probably missing something important, but I am getting no where with this. Does anyone have a thought as to what the issue is, or if this just isn't possible?

Cœur
  • 37,241
  • 25
  • 195
  • 267
saxon564
  • 5
  • 5

2 Answers2

0

Dont use absolute path in include. You have to use relative path to get it work.

<?php include '/tclusa/header.php'; ?>
TBI
  • 2,789
  • 1
  • 17
  • 21
  • 1
    Thank you, that was the exact issue. was just trying to make that code a little more universal so that it could easily be copied from page to page and not need to be modified. Oh well, anyway, thank you so much for the quick response. – saxon564 Aug 01 '14 at 05:05
0

$_SESSION is always set as a PHP Global variable. I don't think you would need to check if the session exists before starting it. And also you can't start the session twice per request (so just remove if from the included header file). You should do a session_start() once at the very beginning of your application and thats it.

If you really do need to check if the session has been initialized use empty(&$var).

Justin Workman
  • 380
  • 3
  • 6
  • ok, thank you for your quick response, the check is just how it was added by my editor the first time I ever use sessions so I was unaware the check was unnecessary. I was also unsure if the session needed to be started in the header.php file since you're suppose to start the session on every page to be able to use sessions. So those unasked questions have also bee answered. :D – saxon564 Aug 01 '14 at 05:11