473,811 Members | 3,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sessions

Any pitfalls or stuff I need to worry about when working with sessions? I
want to write a log file and hit counter along with a login interface and
I'm trying to learn this stuff.

http://us.php.net/session

Just wondering if theres anything that I need to keep in mind while I work
on it?

Thanks,
Jon
Apr 28 '07 #1
3 2737

"Jon Slaughter" <Jo***********@ Hotmail.comwrot e in message
news:Eg******** *********@newss vr19.news.prodi gy.net...
Any pitfalls or stuff I need to worry about when working with sessions? I
want to write a log file and hit counter along with a login interface and
I'm trying to learn this stuff.

http://us.php.net/session

Just wondering if theres anything that I need to keep in mind while I work
on it?

Thanks,
Jon
BTW, I assume this is how the server deals with such things as logins and
stuff? Not sure if I should look into something else?
Apr 28 '07 #2
For clarity: sessions store variables that you want to stay the same
while each person browses your site, the variables are only the same
for the same person, each person has their own session, and when the
session expires (usually after 15 minutes of that person not doing
anything on your website, but that number can be changed in php.ini),
those variables are lost. You have to remember to call session_start()
before using session variables or sending any output to the browser if
you want to make use of the session on a page (it usually sends
cookies to the browser in headers so it can remember the session id
for that person). Sorry if you already know this, I just want to make
sure first.

You usually shouldn't need to worry about the server remembering the
session state, that should probably work without you changing
anything.

If you are on a shared hosting plan, plain sessions may be a security
risk. Session files are normally stored in a common folder that might
not protect your session data from being read by other websites on the
server (on purpose, don't worry about it mixing them up). If you are
running PHP as a CGI binary and can have your own php.ini, be sure
change session.save_pa th (which defaults to /tmp) to somewhere that
only you have access to (if PHP is running as a CGI binary and is
running as your user, you can change the permissions on a directory
you make in your own space to be extra save).

You can also use session_set_sav e_handler() to manage your session
data yourself. You can make functions (or a Session class with methods
for organization) that save the session data into files yourself, or
even to save the serialized session data into a database.
http://us.php.net/manual/en/function...ve-handler.php

You can use sessions to improve your hit counter by saving a variable
when you count that user, so you can count more unique hits. For
example, you could do something like this:
<?php
session_start() ;
if( !isset( $_SESSION['hit_counted'] ) || !$_SESSION['hit_counted'] )
{
file_put_conten ts( 'hits.txt', (int)@file_get_ contents( 'hits.txt' )
+ 1 );
$_SESSION['hit_counted'] = true;
}
?>

Note that for something older than PHP5, you would need to use
different file functions. Also, sessions will not store the count of
hits, you need a file or a database to do that. The above code will
create a hits.txt file if there is not one already. It is important to
remember that it will not count completely unique hits, but just count
once per session. If you want to try to make it completely unique, you
would not need sessions but cookies instead, or you could store IP
addresses in a database. You might as well try Google Analytics
(google.com/analytics) if you want serious traffic analysis (it's
free, and it's just a little JavaScript snippet that connects to
Google).

As for logins: yes, sessions are a good way to remember whether or not
someone is logged in and their user data if your session files are
secure.

-Mike PII

Apr 28 '07 #3

"Mike P2" <su***********@ gmail.comwrote in message
news:11******** **************@ l77g2000hsb.goo glegroups.com.. .
For clarity: sessions store variables that you want to stay the same
while each person browses your site, the variables are only the same
for the same person, each person has their own session, and when the
session expires (usually after 15 minutes of that person not doing
anything on your website, but that number can be changed in php.ini),
those variables are lost. You have to remember to call session_start()
before using session variables or sending any output to the browser if
you want to make use of the session on a page (it usually sends
cookies to the browser in headers so it can remember the session id
for that person). Sorry if you already know this, I just want to make
sure first.

You usually shouldn't need to worry about the server remembering the
session state, that should probably work without you changing
anything.

If you are on a shared hosting plan, plain sessions may be a security
risk. Session files are normally stored in a common folder that might
not protect your session data from being read by other websites on the
server (on purpose, don't worry about it mixing them up). If you are
running PHP as a CGI binary and can have your own php.ini, be sure
change session.save_pa th (which defaults to /tmp) to somewhere that
only you have access to (if PHP is running as a CGI binary and is
running as your user, you can change the permissions on a directory
you make in your own space to be extra save).

You can also use session_set_sav e_handler() to manage your session
data yourself. You can make functions (or a Session class with methods
for organization) that save the session data into files yourself, or
even to save the serialized session data into a database.
http://us.php.net/manual/en/function...ve-handler.php

You can use sessions to improve your hit counter by saving a variable
when you count that user, so you can count more unique hits. For
example, you could do something like this:
<?php
session_start() ;
if( !isset( $_SESSION['hit_counted'] ) || !$_SESSION['hit_counted'] )
{
file_put_conten ts( 'hits.txt', (int)@file_get_ contents( 'hits.txt' )
+ 1 );
$_SESSION['hit_counted'] = true;
}
?>

Note that for something older than PHP5, you would need to use
different file functions. Also, sessions will not store the count of
hits, you need a file or a database to do that. The above code will
create a hits.txt file if there is not one already. It is important to
remember that it will not count completely unique hits, but just count
once per session. If you want to try to make it completely unique, you
would not need sessions but cookies instead, or you could store IP
addresses in a database. You might as well try Google Analytics
(google.com/analytics) if you want serious traffic analysis (it's
free, and it's just a little JavaScript snippet that connects to
Google).

As for logins: yes, sessions are a good way to remember whether or not
someone is logged in and their user data if your session files are
secure.

-Mike PII
Thank you for taking the time out to explain some of the details. The point
you bring up about security worries me. I'm going to have to look into that
more.

What I'm going to do with the hits thing is just use there(the hosts)
statistics page. It gives a much more detailed view and I don't see any
reason to duplicate any code just for it. I'll look into the google thing
though as it seems cool.

Thanks,
Jon

Apr 28 '07 #4

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

Similar topics

2
2821
by: The Plankmeister | last post by:
Hi... I'm trying my hardest to understand fully how sessions work and how best to use them. However, all I can find is information that doesn't tell me anything other than that sessions store information between pages, which I knew already. I want to know HOW sessions work! If anybody has any good links to material that explains sessions fully, then please send those links this way! I'm particularly interested in the...
13
12055
by: jing_li | last post by:
Hi, you all, I am a newbee for php and I need your help. One of my coworker and I are both developing a webpage for our project using php. We have a copy of the same files in different location on the server (in our own accounts on the same machine). When I am testing both versions of our program using the same browser (IE on Windows or Konqueror on Linux) the session variables will mix up and only the latest selection or options will...
3
2485
by: Maxime Ducharme | last post by:
Hi group We have a problem with sessions in one of our sites. Sessions are used to store login info & some other infos (no objects are stored in sessions). We are using Windows 2000 Server (IIS 5.0) with ASP 3.0 (no .NET on this site). Sometime, data in session is emptied. I say "sometime"
3
3723
by: Will Woodhull | last post by:
Hi, I'm new here-- I've been reading the group for a couple of days. Nice group; I like the way n00b33 questions are handled. I've been using a Javascript routine in index.html to determine a visitor's browser's capabilities. The Javascript then calls main.php, passing back its findings with a GET string; main.php saves the data as a visitor's profile in $_SESSION elements. It then serves up home.html and any further pages requested...
2
2974
by: Steve Franks | last post by:
According to the docs you tell ASP.NET to use cookieless sessions by setting a value in the config.web file. However, what if I wanted to determine at run time whether or not I wanted to use cookieless sessions for a particular user, and if so, I'd instruct ASP.NET to turn on cookieless sessions for a particular user session. Is this possible? For example I want to use cookie based sessions by default for all users. But if a user has...
12
2297
by: D. Shane Fowlkes | last post by:
This is a repost (pasted below). Since my original post, I've double checked the system clock and set all IIS Session Timeout values to 10 minutes. Still ...the problem occurs. I've also installed Deep Metrix Live Stats 6.2 XPS just to make sure nothing really strange was going on was going on. Still....the sessions only increment...or should I say my counter in my asax file.... and never goes down. If it matters, this machine is...
6
3812
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for one day): \\FFDS24\ASP.NET Applications(_LM_W3SVC_1_Root_ATV2004)\Errors During Execution: 7 \\FFDS24\ASP.NET Apps v1.1.4322(_LM_W3SVC_1_Root_ATV2004)\Compilations
22
3189
by: magic_hat60622 | last post by:
Hi all. I've got an app that dumps a user id into a session after successful login. the login page is http://www.mydomain.com/login.php. If the user visits pages on my site without the www (i.e., http://mydomain.com/foo.php), the session works fine and login state is maintained. If he visits http://www.mydomain.com/foo.php, the app drops the logged-in state.
13
35996
Frinavale
by: Frinavale | last post by:
One of the most fundamental topics in web design is understanding how to pass information collected on one web page to another web page. There are many different ways you could do this: Cookies, Database... However, I'm going to cover how to use Sessions. Sessions are used to store information in order to use it during later page requests or in other web pages in a web application. By default Cookies are used to identify which session...
3
28336
Atli
by: Atli | last post by:
Introduction: Sessions are one of the simplest and more powerful tools in a web developers arsenal. This tool is invaluable in dynamic web page development and it is one of those things every developer needs to know how to use. This article explains the basics of PHP Sessions. Assumptions: Basic PHP knowledge is required (variables, arrays and such) HTML Forms. What are Sessions? Sessions are a way of storing data. When developing...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.