Page 1 of 1

Sitewide Sessions Handling

Posted: Tue Nov 11, 2008 4:00 am
by clix99
Hey everyone, i'm new here and hope to learn and help as much as i can :D

I have an unsolved issue on my site, which is a little complicated to explain, hope it as clear enough:

I built a PHP file (count.php) that handles sessions - the file create a new session variable every time a visitor come to the site.
If the variable exists already it adds 1 to this variable - this is the page views counter:

session_start();
if(isset($_SESSION['count']))
$_SESSION['count'] = $_SESSION['count'] + 1;
else
$_SESSION['count'] = 1;

The count.php file works fine - i also duplicated it as count2.php, and when i surf from count.php to count2.php i see the page views increasing.

I wanted this function to be on my whole site so i did:
<?include 'count.php'?> on all of my pages.

And here is the problem - It seems like every page view the session info from the last page doesn't pass to the next page and i always get
$_SESSION['count'] as 1.

Any idea why it's like this?

I tried doing this:
<script src='count.php'></script>
On my pages - this works fine but it doesn't help me with what i want. I want to be able to do different stuff on the php level on my site's pages with the session variables. So i need to get the value of the session variables on my php code and not as an external file.

Hope someone can help me...

Re: Sitewide Sessions Handling

Posted: Tue Nov 11, 2008 4:55 am
by aceconcepts
Remeber that you must call session_start() from each page (at the top) where you intend to use sessions.

Also I noticed that you don't have a semi-colon at the end of you include (maybe you do in your actualy script).

One other thing, you could slightly optimise you conditional statment like this:

Code: Select all

 
session_start();
if(isset($_SESSION['count']))
$_SESSION['count'] += 1; //i find it quite handy
else
$_SESSION['count'] = 1;
 

Re: Sitewide Sessions Handling

Posted: Tue Nov 11, 2008 6:42 am
by clix99
Thanks aceconcepts,
I do have a semi-colon there, and use the session_start() at the beginning of the code.

I did a few checks and got the following results.

Like i said, i have a php file that handles sessions - this time i added an option that adds a row to a database table each time a new session is opened. This means that only if the $_SESSION['count'] is empty it gets the value 1 and i create a new row on a database.

Here are 3 option i tried:

1. I added this code to all my site pages:
<?include 'count.php';?>
Results:
when i surf on pages that are on my root directory for example: http://www.mydoamin.com, http://www.mydoamin.com/about-us.php, http://www.mydoamin.com/contact-us.php.. It works fine.

When i surf to pages that are under directories: http://www.mydoamin.com/info/index.php, http://www.mydoamin.com/network/123.php... A New session is open. It open a new session every time i go from a file in the root to one in a directory... :?

2. i replaced <?include 'count.php';?> with <?include 'http://www.mydomain.com.count.php';?>
Results:
In this case every page view creates a new row in the database...

3. I added the code manually to all files:
Results:
This works fine!!!!


What is the conclusion?
I have one - when you do "Includes" it's not exactly like writing the code...

How can i do an include and still get the same results in my case?

Thanks.

Re: Sitewide Sessions Handling

Posted: Tue Nov 11, 2008 6:53 am
by aceconcepts
When you navigate to subfolders just as you illustrated you need to include the coun.php file relative to its location.

Like this:

In this url: http://www.mydoamin.com/info/index.php you must include the file as

Code: Select all

<? include"../count.php"; ?>
"../" means navigate up one level.

Hope it helps.

Re: Sitewide Sessions Handling

Posted: Tue Nov 11, 2008 9:36 am
by clix99
Thanks again aceconcepts,

That did the trick.

What about directories within directories:

How should the include be for these file:

http://www.mydoamin.com/info/sub/index.php

?

Re: Sitewide Sessions Handling

Posted: Tue Nov 11, 2008 9:44 am
by mmj
clix99 wrote:Thanks again aceconcepts,

That did the trick.

What about directories within directories:

How should the include be for these file:

http://www.mydoamin.com/info/sub/index.php

?
Then just go up one more level

Code: Select all

<?php include"../../count.php"; ?>
 
If its starting to get ugly then use absolute paths
 

Code: Select all

<?php include '/home/user/public_html/inc/count.php'; ?>
 
aceconcepts wrote:

Code: Select all

 
session_start();
if(isset($_SESSION['count']))
$_SESSION['count'] += 1; //i find it quite handy
else
$_SESSION['count'] = 1;
 
 
wouldn't this work?
 

Code: Select all

session_start();
$_SESSION['count'] = isset($_SESSION['count']) ? $_SESSION['count']++ : 1
 

Re: Sitewide Sessions Handling

Posted: Tue Nov 11, 2008 10:38 am
by aceconcepts
That would work indeed :D