473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Establishing PHP session ID in an included file

Forgive me if this doesn't make too much sense but I've been working on this
project all day and my brain is a bit fried.

To make some of my code more readable I made a file (called urls.inc) which
I include in my PHP scripts. This file contains functions to return a URL
with the session id tagged on the end. I've now found out that the session
id returned by the functions in urls.inc is not necessarily the current, and
more importantly correct, session id. I need to know if there is a way to
establish the current session id within the included file.

For info, I've built the scripts so that the session id is passed from page
to page to avoid using cookies (part of the challenge, it's a college
project) so I need the urls.inc file to know which session id to pass back
otherwise it all goes a bit squiffy.

Here's urls.inc :

<?php

session_start();

function logoutURL()
{
return "logout.php?PHPSESSID=" . session_id();
}

function adminURL()
{
return "admintools.php?PHPSESSID=" . session_id();
}

function homeURL()
{
return "main.php?PHPSESSID=" . session_id();
}

?>

These are called like this :
echo "<h3 align=\"center\"><a href=\"" . adminURL() . "\"
target=\"mainFrame\">Administration Tools</a></h3>";

If it turns out that there's no way to do what I'm after then it's not big
deal because I can change the URL line to :
echo "<h3 align=\"center\"><a href=\"admintools.php?PHPSESSID=" .
session_id() . "\" target=\"mainFrame\">Administration Tools</a></h3>";

I just wanted to show some initiative and keep the code as tidy and as
possible.

Thanks for any help,

Pete.
Jul 17 '05 #1
3 2538

"Peter Redding" <skredding_NOSPAM_@.netscape.net> wrote in
message
news:WP*****************@fe2.news.blueyonder.co.uk ...
Forgive me if this doesn't make too much sense but I've been working on this project all day and my brain is a bit fried.

To make some of my code more readable I made a file (called urls.inc) which I include in my PHP scripts. This file contains functions to return a URL with the session id tagged on the end. I've now found out that the session id returned by the functions in urls.inc is not necessarily the current, and more importantly correct, session id. I need to know if there is a way to establish the current session id within the included file.

For info, I've built the scripts so that the session id is passed from page to page to avoid using cookies (part of the challenge, it's a college project) so I need the urls.inc file to know which session id to pass back otherwise it all goes a bit squiffy.

Here's urls.inc :

<?php

session_start();

function logoutURL()
{
return "logout.php?PHPSESSID=" . session_id();
}

function adminURL()
{
return "admintools.php?PHPSESSID=" . session_id();
}

function homeURL()
{
return "main.php?PHPSESSID=" . session_id();
}

?>

These are called like this :
echo "<h3 align=\"center\"><a href=\"" . adminURL() . "\"
target=\"mainFrame\">Administration Tools</a></h3>";

If it turns out that there's no way to do what I'm after then it's not big deal because I can change the URL line to :
echo "<h3 align=\"center\"><a href=\"admintools.php?PHPSESSID=" . session_id() . "\" target=\"mainFrame\">Administration Tools</a></h3>";
I just wanted to show some initiative and keep the code as tidy and as possible.

Thanks for any help,

Pete.

Hey Pete,

A couple things: First I think PHP 4.x and higher suggest
using the $_SESSION global array

$_SESSION ['session_id']

Second, if I read
http://us2.php.net/manual/en/ref.session.php correctly, then
you would have a new session id every time you run the app
because you have never set a session variable.
In general you want to do something like this:

web page 1:
session_start();
$_SESSION['some_session_var'] = "This variable is now set -
was done so with session_id = ".$_SESSION['session_id'];

web page 2:
session_start();
$temp = $_SESSION['some_session_var'];

print " 'some_session_var' was previous set to '$temp'";
print " <br>The session_id is now ".
$_SESSION['session_id'];
-CF

Jul 17 '05 #2

"ChronoFish" <de**@chronofish.com> wrote in message
news:2rqPc.18425$cv5.3480@lakeread07...

<Snip my previous post>
Hey Pete,

A couple things: First I think PHP 4.x and higher suggest
using the $_SESSION global array

$_SESSION ['session_id']

Second, if I read
http://us2.php.net/manual/en/ref.session.php correctly, then
you would have a new session id every time you run the app
because you have never set a session variable.
In general you want to do something like this:

web page 1:
session_start();
$_SESSION['some_session_var'] = "This variable is now set -
was done so with session_id = ".$_SESSION['session_id'];

web page 2:
session_start();
$temp = $_SESSION['some_session_var'];

print " 'some_session_var' was previous set to '$temp'";
print " <br>The session_id is now ".
$_SESSION['session_id'];
-CF


First things first : Now that my brain is slightly less frazzled today I can
tell you that I am using PHP 4.3.7 on Apache 2 under Mandrake Linux 8.2.

I am passing through the session id via the URL into the second page (and so
on) and use session_start() in subsequent pages so that means that it
doesn't start a brand new session with a new session id and allows me access
to all my previously stored session variables (unless I unset them or
destroy the session). At least that is my understanding and that is what
has been happening.

So is session_id() now superseded by $_SESSION['session_id']? Not that it
seems to make much difference as session_id() is still working for me but if
using the other method is recommended then I would change my code.

For info, I tried to pass through the session id into the function call,
store it in a local variable and return it with the URL but it didn't seem
to work (the session id returned was still different to the session id in
some hard coded URLs). As I said previously I was just trying to show some
initiative but I'll probably just hard code the URLs in for now as I don't
want to get hung up on something which is trivial in comparison to some of
the other things I have done (and I am running out of time!!!).

Thanks again,

Pete.
Jul 17 '05 #3
<SNIP>
First things first : Now that my brain is slightly less frazzled today I can tell you that I am using PHP 4.3.7 on Apache 2 under Mandrake Linux 8.2.

I am passing through the session id via the URL into the second page (and so on) and use session_start() in subsequent pages so that means that it
doesn't start a brand new session with a new session id and allows me access to all my previously stored session variables (unless I unset them or
destroy the session). At least that is my understanding and that is what
has been happening.

So is session_id() now superseded by $_SESSION['session_id']? Not that it
seems to make much difference as session_id() is still working for me but if using the other method is recommended then I would change my code.

For info, I tried to pass through the session id into the function call,
store it in a local variable and return it with the URL but it didn't seem
to work (the session id returned was still different to the session id in
some hard coded URLs). As I said previously I was just trying to show some initiative but I'll probably just hard code the URLs in for now as I don't
want to get hung up on something which is trivial in comparison to some of
the other things I have done (and I am running out of time!!!).

Thanks again,

Pete.


Okay, I've had one of those "Now it works, but should it?" experiences. The
problem arose in the first bit I tested so I assumed that I couldn't do what
I was trying, period. However, the part that I was testing was a set of
search results from a database based on criteria posted from a form on a
separate page. When the form was posted to the searchresults.php page I
wasn't passing through the session id but as soon as I added that to the URL
in the action part of the form, it worked.

Now the session id is the same when returned from urls.inc as it is if I
"hard code" it in to the script using "<URL>.php?PHPSESSID=" . session_id()
so it seems as though the included file will pick up the correct session
details and as it works, I'm not going to fiddle with it anymore.

If only I'd tested another bit first....
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: BlueFrog | last post by:
Hey, Anyone got any ideas why I wouldn't be able to output a value stored in the session array in a particular include file - I am using session_start() but to no avail. Joe
1
by: Sean Pinto | last post by:
Ok, you all are going to have to bear with me on this one as it is kinda complicated to explain. I am implementing a company management suite that requires Role-Based authentiations (ie. users are...
7
by: Bonj | last post by:
Hi I have a mixed managed/unmanaged project which thanks to you guys I've managed to get rid of the linker errors of, so cheers for that. But now I'm experiencing an unknown access violation....
1
by: Werner | last post by:
Hi Patrick! Can you give an example of how to use a frameset inside an aspx-file? When I create a new frameset in Visual Studio.Net it just gives me a htm-File. Or give me a link where I can...
6
by: KevinGravelle | last post by:
What is wrong with this picture? I'm trying to set my shopping cart text on my home page using the following function that is executed when the class is constructed: protected string...
5
by: google | last post by:
I have a website that has a asp secured members only aria that keeps session variables to check if someone is logged in or not (if session variables are not there then redirect to logon screen) but...
3
by: damezumari | last post by:
To find out were session variables are stored I included this instruction in my program: echo ini_get("session.save_path"); The reply was /home/7604/data/tmp which is a folder on my server. I...
10
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
I've been on this for a while, and cannot figure it out. Can someone please help with this message? SessionState can only be used when EnableSessionState is set to true, either in a...
4
by: Cirene | last post by:
In my web.config I added <pages enableSessionState="true">. In each of my pages I also added EnableSessionState="True" to the Page declaration. (I didn't think this was necessary, but...) ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.