By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

session handling

P: n/a
lak
I want to study about the Session handling in PHP. I don't know where
to start. So please suggest me some way.
Aug 29 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
lak wrote:
I want to study about the Session handling in PHP. I don't know where
to start. So please suggest me some way.
I'm not sure what you're looking for. Basic session handing is quite
simple - at the start of any page which uses sessions call
session_start(), before ANY output is sent to the browser. After that,
just set values in the $_SESSION[] array and later retrieve them from
the $_SESSION[] array. PHP handles the rest.

Of course, if you want to get into custom session handlers, that gets a
bit more complicated.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 29 '08 #2

P: n/a
On Aug 29, 10:14*am, lak <lakindi...@gmail.comwrote:
I want to study about the Session handling in PHP. I don't know where
to start. So please suggest me some way.
http://php.net/session
Aug 29 '08 #3

P: n/a
I want to study about the Session handling in PHP. I don't know where
to start. So please suggest me some way.
If you haven't discovered it yet, phonet is one of the best resurces
around for anything to do with PHP et al. Try http://php.net/session
for one, and then there is a lot of info at w3schools too.
If you become familiar with those sites you'll have a wealth of
startup information and more available to you.

HTH

Aug 29 '08 #4

P: n/a
lak wrote:
>I want to study about the Session handling in PHP. I don't know where
to start. So please suggest me some way.

I'm not sure what you're looking for. Basic session handing is quite
simple - at the start of any page which uses sessions call
session_start(), before ANY output is sent to the browser. After
that, just set values in the $_SESSION[] array and later retrieve
them from the $_SESSION[] array. PHP handles the rest.

Of course, if you want to get into custom session handlers, that gets
a bit more complicated.
But J, it's not very simple to a beginniner. In fact, just the phrase
"before any output is sent to the browser" can create some lengthy
conversations for those who don't yet have experience in that area.
Even Hello World programs take a certain amount of introduction for
newbies to PHP.

Why is it you chose to not respond to his question but instead made
vague generalized statements?

I'm curious.

Twayne
Aug 29 '08 #5

P: n/a
On Fri, 29 Aug 2008 06:14:06 -0700 (PDT), lak <la********@gmail.com>
wrote:
>I want to study about the Session handling in PHP. I don't know where
to start. So please suggest me some way.
I'm a PHP newbie like you, but here's some working code to give you an
idea:

Add this to every page that is off-limit to non-authorized users:

<? // Has a session already been created? If not, create new one
if($PHPSESSID)
session_start($PHPSESSID);
else
session_start();
?>

Here's how to manipulate data that are part of a session:
<? session_register("email"); ?>
<? $email="me@acme.com"; ?>
<? echo $email; ?>
<? session_unregister("email"); ?>
<? session_destroy(); ?>

Here's how to extract information form a session table:
$sql = "select user_id,status,date_created from session where
id='" . $PHPSESSID . "'";
$result = @mysql_query($sql) or
die('Query failed: ' . mysql_error());

$row = mysql_fetch_row($result);
echo "user_id = " . $row[0] . "<p>";
echo "status = " . $row[1] . "<p>";
echo "date_created = " . $row[2] . "<p>";

If most data are common to all users, a smarter way is to keep
user-specific data in sessions, but keep
common data in a cache (APC, MemCacheD, etc.):
session_start();
if(isset($_SESSION['myprivatevalue'])) {
print $_SESSION['myprivatevalue'] . "<p>\n";
} else {
$_SESSION['myprivatevalue'] = "verysecret";
}

//apc_add('scooby-doo', 'daphne');
print "Scooby-do=" . apc_fetch('scooby-doo');
//apc_delete('scooby-doo');

http://www.tizag.com/phpT/phpsessions.php

HTH,
Sep 1 '08 #6

P: n/a
..oO(Gilles Ganault)
>On Fri, 29 Aug 2008 06:14:06 -0700 (PDT), lak <la********@gmail.com>
wrote:
>>I want to study about the Session handling in PHP. I don't know where
to start. So please suggest me some way.

I'm a PHP newbie like you, but here's some working code to give you an
idea:
Some notes about this "working" code:
>Add this to every page that is off-limit to non-authorized users:

<? // Has a session already been created? If not, create new one
Avoid short open tags. They are completely unreliable and will most
likely be turned off by default in the coming PHP 6.
if($PHPSESSID)
Where is $PHPSESSID coming from? And why are you interpreting it as a
boolean?
session_start($PHPSESSID);
else
session_start();
?>

Here's how to manipulate data that are part of a session:
<? session_register("email"); ?>
session_register() is deprecated and not necessary anymore.
<? $email="me@acme.com"; ?>
<? echo $email; ?>
<? session_unregister("email"); ?>
Same here. Just drop it.
<? session_destroy(); ?>
And why all the <? ... ?>? Why not simply a single <?php ... ?block?

To summarize all the above:

<?php
session_start();
$_SESSION['email'] = 'm*@example.com';
?>

That's it. Then on another page:

<?php
session_start();
if (isset($_SESSION['email'])) {
print $_SESSION['email'];
}
?>

Or something like that.
>Here's how to extract information form a session table:
$sql = "select user_id,status,date_created from session where
id='" . $PHPSESSID . "'";
The next problem. Even a session ID should be handled with care and be
seen as a potential threat. _Never_ trust anything coming in from the
client! The keyword here is "SQL injection".

Micha
Sep 1 '08 #7

P: n/a
Gilles Ganault wrote:
On Fri, 29 Aug 2008 06:14:06 -0700 (PDT), lak <la********@gmail.com>
wrote:
>I want to study about the Session handling in PHP. I don't know where
to start. So please suggest me some way.

I'm a PHP newbie like you, but here's some working code to give you an
idea:

Add this to every page that is off-limit to non-authorized users:

<? // Has a session already been created? If not, create new one
if($PHPSESSID)
session_start($PHPSESSID);
else
session_start();
?>

Here's how to manipulate data that are part of a session:
<? session_register("email"); ?>
<? $email="me@acme.com"; ?>
<? echo $email; ?>
<? session_unregister("email"); ?>
<? session_destroy(); ?>

Here's how to extract information form a session table:
$sql = "select user_id,status,date_created from session where
id='" . $PHPSESSID . "'";
$result = @mysql_query($sql) or
die('Query failed: ' . mysql_error());

$row = mysql_fetch_row($result);
echo "user_id = " . $row[0] . "<p>";
echo "status = " . $row[1] . "<p>";
echo "date_created = " . $row[2] . "<p>";

If most data are common to all users, a smarter way is to keep
user-specific data in sessions, but keep
common data in a cache (APC, MemCacheD, etc.):
session_start();
if(isset($_SESSION['myprivatevalue'])) {
print $_SESSION['myprivatevalue'] . "<p>\n";
} else {
$_SESSION['myprivatevalue'] = "verysecret";
}

//apc_add('scooby-doo', 'daphne');
print "Scooby-do=" . apc_fetch('scooby-doo');
//apc_delete('scooby-doo');

http://www.tizag.com/phpT/phpsessions.php

HTH,
Or simply get the correct code here:
http://www.php.net/manual/en/book.session.php

Pretty explanatory there.
Sep 1 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.