Forum Moderators: coopster

Message Too Old, No Replies

Process sessions in full index with variables?

Is it possible to process PHP sessions in full index while using variables?

         

mcbrook

10:41 pm on Nov 23, 2007 (gmt 0)

10+ Year Member



Hi,

I am processing PHP sessions through a specific page called login_check.php. This page checks to make sure the user is logged in. If the user is logged in, it keeps the session open, but if not logged in, it deletes the session.

Then, I have a page called header_main.php. This page is a dynamic header which I include in all of my pages. In this page, I include the login_check.php page in order to display the user's login status in the header of my pages.

My problem is that, in order to process the sessions through my header_main.php page, I would have to include my header with the full document root, which looks like this:

include($_SERVER["DOCUMENT_ROOT"] . '/layout/header_main.php');

Because this will not correctly process the sessions:

include('http://domain.com/layout/header_main.php');

This would be fine if I left it like this, however, I also need to pass variables through my header_main.php page, which would look something like this using the working method:

include($_SERVER["DOCUMENT_ROOT"] . '/layout/header_main.php?home=true&p=h&error=1');

These variables control the page navigation styles for different pages and display specified error messages for certain errors. However, this include function will display an error because it is trying to find a page called "header_main.php?home=true&p=h&error=1" which does not exist.

So now for the question, is there any way that I can successfully process sessions through my header but still attach variables on my page address to include my header on all my pages?

PHP_Chimp

7:33 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Am I correct in assuming that on your header_main.php page you are using the $_GET array to pass values to functions on that page?

Is so then cant you rewrite those functions to take values input directly?
i.e.


function OLD_nav() {
if ($_GET[home] == true) {
// echo's nav bar highlighting home
}
}
function NEW_NON_GET_nav($home = 1) {
if ($home == 1) {
// echo's nav bar highlighting home
}
}

Then you can pass your variables directly.

include($_SERVER["DOCUMENT_ROOT"] . '/layout/header_main.php');
echo NEW_NON_GET_nav(1); // or even just echo NEW_NON_GET_nav();
// or if not home then -
echo NEW_NON_GET_nav(0)

I know that means more code to write, but it doesnt use the query string, so should stop your problem with the server hanging on each request. There may be a better way...but I have used this method in quite a few places.

mcbrook

8:03 am on Nov 25, 2007 (gmt 0)

10+ Year Member



Thanks. That is actually a good idea. It should work in theory. It would take a little more coding but I think that is one of the easiest ways of handling it believe it or not. It seems strange that I have this problem simply because of the syntax but I guess it is just the way it works unfortunately.

ashishp

8:16 am on Nov 25, 2007 (gmt 0)

10+ Year Member



You can also set the variable BEFORE including the file.

$home = true;
$p = 'h';
$error = '1';
include("$_SERVER[DOCUMENT_ROOT]/layout/header_main.php");

The variables will be available to the included script, just change the

if ($_GET[home] == true) {

to
if ($home == true) {

mcbrook

8:52 am on Nov 25, 2007 (gmt 0)

10+ Year Member



That is true too. That may actually be even easier. The only problem I see with that though is if I don't want to apply the same variables to multiple headers in my page that are separated by if else statements depending on factors such as errors for displaying different error messages. But I'm sure there's an easy workaround for that.

ashishp

10:07 am on Nov 25, 2007 (gmt 0)

10+ Year Member



That is just a matter of adopting good coding standards and also planning your code/variables correctly. Do some reading on php coding best practices so that maintaining your code becomes very easy.

Assuming you want to highlight the current section on the menu:

You can set a variable called $current and set it to the section like so:

$current = 'home' or $current = 'news' or whatever

and then use a switch statement to highlight the section.

This gives me complete flexibility to choose the section to be highlighted.

mcbrook

1:09 am on Nov 27, 2007 (gmt 0)

10+ Year Member



Thanks. I'll give that a try.