- PeterMoulding.com
- Author
- Trainer
- Speaker
- Business Coach
- How to write a How To book
- PHP Courses
- Speaking
- Web Architect
- Australia
- Books
- Authors
- Akkana Peck
- Alex Berenson
- Andrew Nugent
- Ben Sanders
- Brock Clarke
- Chris Simms
- David Mercer
- Dianna Mullet
- Don Winslow
- Dori Smith
- Harlan Coben
- Jack McDevitt
- James Wines
- Jerry Yudelson
- John Grisham
- Kevin Mullet
- L. E. Modesitt Jr.
- Laurell K. Hamilton
- Marshall Karp
- Martina Cole
- Michael Marshall Smith
- Michel Roux Jr
- Nadia Sawalha
- Philip Pullman
- Raymond Khoury
- Richard North Patterson
- Robert Masello
- Sally Roth
- Sarah Langan
- Stella Rimington
- Stephen Booth
- Stephen King
- Stephen Leather
- T.C. Boyle
- Tom Negrino
- Tony Hillerman
- Urban Waite
- Val McDermid
- Valerio Massimo Manfredi
- Beginning GIMP
- Beginning Visual C++
- Culturalism
- Fiction
- A Drink Before The War
- A Talent for War
- Bag of Bones
- Blood and Ice
- Burn
- Dark Lady
- Dead Line
- Eclipse
- Empress of Eternity
- Exley
- Flipping Out
- Just One Look
- Nightfall
- Pet Sematary
- Savage Moon
- Skinwalkers
- Starvation Lake
- The Fallen
- The Gardens of the Dead
- The Jump
- The Last Templar
- The Mermaids Singing
- The Midnight Mayor
- The Secret Soldier
- The Summons
- The Terror of Living
- The Testament
- The Tower
- Under the Dome
- Virus
- AJAX and PHP
- Aging with Grace
- Food books
- Green Architecture
- Life Is So Good
- SQL: The Complete Reference
- The Backyard Bird Lover's Ultimate How-to Guide
- The Garden Gurus
- Authors
- Sustainability
- -18 hours left to decide the future of Australia
- Campbells vegetable stock or Massel vegetable stock?
- Carbon Sequestration
- Carbon tax for Australia is a fraud
- Copenhagen will fail
- Cost of living in Australia
- Dick Smith jumps on the population bandwagon
- Dry Run: Preventing the Next Urban Water Crisis
- Energy Saving Lights
- Garlic
- How many people can live in Australia?
- Its obsolete, throw it out!
- Julia Gillard offers 9.9 billion dollars bribe to Rob Oakeshott
- Laundry detergent
- Petrol or Diesel?
- Reflective foil batts kill
- RoHS
- Sea level to rise 3mm due to climate change
- Solar power
- Spring again in Sydney
- Sustainable fuels
- The CRUD Tax is back
- The people who make building regulations do not own houses
- Water efficiency
- Which insulation is safer, foil or wool?
- Will Australia reduce greenhouse gas emissions?
- Technology
- Android or Blackberry or iPhone or a flip phone?
- Apple versus Google 2011
- Cameras
- Cars
- Colour
- Burgundy
- Colour Blindness
- Colour Names
- Dulux colours
- Pantone colours
- Safe Colours
- Seculine ProDisk Mini colour balance card
- What Causes Colour Blindness?
- Hardware
- Batteries for the Digital Age
- Cables
- Cases
- Computer reliability
- Computrace
- Disks
- Astone ISO Gear 481E
- Best SSD for your notebook computer
- Disk block size
- Hitachi disk HDS722020ALA330
- LaCie USB 2.0 250 GB mobile hard drive design by F.A. Porsche
- SMART disk
- Samsung 2 TB HD204UI quiet low power disk for mass storage
- Seagate and Samsung merge disk business
- Select the right disk for your RAID array
- USB disk speed
- Western Digital WD20EARX 2 GB SATA 3 disk
- How long should computer hardware last?
- Keyboards
- Mainframe
- Memory cards
- Monitors
- Netbooks, notebooks, tablets, and xPads
- Network Attached Storage
- OLED Displays
- PC's are a thing of the past
- Printers
- Quiet
- Samsung Galaxy S
- Speed
- Television
- Tools
- USB
- Worst computer movies
- Xserve is dead. What next?
- Your backup will not work
- Z68 motherboards
- iPad or Acer Aspire One?
- IQ
- LG Intello Washing Machine
- Lack of a challenge
- Networks
- 802.11n wireless networking
- D-Link DIR-655 wireless router
- D-Link DWA-160 Xtreme N dual band USB adapter
- D-Link DWA-556 Xtreme N PCI Express desktop adapter
- MIMO
- NBN spends another $12 billion of our tax money on nothing
- National Broadband Network
- Netgear wireless modem router DGND3300 with 300 Mbps 802.11n
- Refrigerator kills wireless broadband
- Small Wireless Network
- TP-LINK TL-SG10005D 5 port gigabit switch
- TP-Link TL-WR1043N wireless N gigabit router
- Telstra Pre-paid Mobile Wi-Fi
- Where are the router plus proxy server combinations?
- Open Source documentation
- Software
- 7-zip
- Accounting
- Asterisk
- Audacity
- Backup software
- Bloat only in Windows
- CAD
- CDex
- Disk imaging software for copying and backup
- Exact Audio Copy
- Filezilla
- Firefox
- Java
- LibreOffice or OpenOffice?
- Linux
- 1 in 5 servers will ship with Linux
- Android phones outsell iPhone
- Another Move to Linux
- CentOS 5.5 installation on SSD and RAID 5
- Debian
- Debian 5.0.5 AMD64 installation
- Debian 5.06 installation
- Fedora
- Fedora or Ubuntu?
- Gnome or KDE?
- K9copy
- Linux 2.6.38
- Linux Gnome login settings lost
- Linux Mint
- Linux RAID, a rant
- Linux Speed
- Linux Time
- Linux reliability as demonstrated by Ubuntu 10.10
- Linux reliability as demonstrated by Ubuntu 11.4
- Linux still a struggle in 2011
- Linux workstation disk RAID 1
- Linux, NT, Windows, and SETI
- Linux, three years of progress
- London Stock Exchange switches to Linux
- Mandrake Linux 9.2
- The partition is misaligned by 48128 bytes - warning from Linux RAID
- Ubuntu
- How to fix the scroll bars in Ubuntu 11.4 Gnome
- Kubuntu 10.10 alternate installation on desktop with RAID 1
- POWbuntu
- Ubuntu 10.10 after 6 months use
- Ubuntu 10.10 alternate installation
- Ubuntu 10.10 desktop RAID 1
- Ubuntu 10.10 desktop RAID 5
- Ubuntu 10.10 desktop install on a netbook
- Ubuntu 10.10 desktop installation
- Ubuntu 10.10 netbook install on a netbook
- Ubuntu 10.10 server AMD64
- Ubuntu 10.10 upgrade to version 11.4 beta 2
- Ubuntu 10.4
- Ubuntu 11.10
- Ubuntu 11.10 first upgrade
- Ubuntu 11.4 after one month use
- Ubuntu 12.04 beta1 desktop amd64
- Ubuntu One
- Ubuntu by Microsoft?
- Ubuntu desktop upgrade 10.4 to 10.10 failed because I did not check the media
- Ubuntu strikes again
- Upgrade Ubuntu to Linux Mint 12 LDXE for extra speed
- Yes, use Linux but not that distribution!
- Nero
- OpenOffice
- OpenOffice is now Apache Office
- Project management
- Scribus
- Software for Windows and Linux
- Text editors
- Time
- Todo applications
- Tomboy notes
- Top text editors
- Version control
- VideoLAN VLC media player
- Visio
- Webmin
- Webmin installation on CentOS for Web development
- Webmin installation on Ubuntu
- What is the most popular open source software today?
- Windows
- Another Windows person goes Linux
- BAD_POOL_CALLER
- Cygwin
- Microsoft Malicious Software Removal Tool cannot find a common virus
- One of the developers of Windows XP is criminally insane
- There are unused icons on your desktop
- W32time
- Which Windows version?
- Windows 7 Home Premium
- Windows XP Stop 0x0000007B during installation
- Windows XP is a disaster
- Windows processes
- XML
- Zip, bzip, gzip, or 7zip?
- configFree
- Technology Succession Planning
- VoIP
- Web Sites
- Drupal
- Do Drupal themes have to use the GPL?
- Drupal 7
- A better search facility for Drupal
- Drupal - performance or flexibility
- Drupal 7 Fields are hard to fix
- Drupal 7 new features
- Drupal 7 ships on January 5
- Drupal 7.14
- Drupal 7.4 hits PeterMoulding.com
- Drupal function sequence
- The evolution of a module
- Undefined index: headers in DefaultMailSystem->mail() (line 54 of /modules/system/system.mail.inc).
- Undefined index: to in DefaultMailSystem->mail() (line 83 of /modules/system/system.mail.inc).
- implode(): Invalid arguments passed in DefaultMailSystem->format() (line 23 of /modules/system/system.mail.inc).
- Drupal 8
- Drupal Code Load Cut
- Drupal How To
- Drupal Modules
- Backup and Migrate
- Browscap
- CKEditor with Drupal WYSIWYG
- Captcha
- Cel
- Colorbox
- Content Construction Kit
- Content type
- Devel module for Drupal
- Drupal Rules as an automation language
- Drupal Spam add-on module
- Form alter to node
- IMCE
- IMCE Wysiwyg bridge
- ImageAPI
- Jdog
- Lightbox2
- Module variable
- Node Gallery Access
- Node_Gallery
- Path
- Path redirect
- Pathauto
- Pet
- Search
- Service links
- Session Variable
- Statistics
- Taxonomy
- Token
- Token ex
- Transliteration
- Trigger
- Watch
- Other modules
- Drupal Training
- Drupal access controls need a major rewrite
- Drupal coding tricks
- Drupal performance
- Drupal themes for the future
- Drupal.org colours
- Import existing data into Drupal
- Multiple Web sites made easy using Drupal multisite and the right start
- drupal_lookup_path()
- Adobe PDF
- Apache
- Apache Mahout
- Audi.com
- Bleet
- CSS Strikes Again
- CSS or xCSS
- Can you believe Facebook or email?
- Content Management Systems
- Databases
- Facebook scam
- Font
- Fonts
- HTML
- Install Apache, MySQL, and PHP 5 in Ubuntu 11.4 using the Ubuntu Software Centre
- Language Codes
- Marketing
- Memcache
- Nginx
- Open source development hits another roadblock
- Oscars
- PHP
- SPDY
- Search software
- Techoni.com.au
- Theme themes
- Things to hate on Web sites
- U.S. Patent No. 6,985,875
- Virtual Private Server
- Visible Improvement
- Web 4.0
- Web browser usage
- Web browsers
- Web site development
- Bluefish
- Crying over spilt code
- Eclipse and PHP
- Getting a Git client, a story of ancient technology and pain
- HTTrack
- MVC
- Netbeans
- PHP or ..., CakePHP/Symfony/ZF versus ...
- Programming
- Superfish
- Web browser emulators for testing your Web site
- Web development frameworks
- Web site books
- Web site development on your own computer
- Webmin or phpMyAdmin or cPanel for creating databases?
- aiki framework
- jQuery
- Views development - Learn Fields first
- Views development - Learn Actions and Rules
- jQuery .each()
- jQuery .has()
- jQuery .is()
- jQuery and Firefox Firebug
- jQuery children
- jQuery for people not using Drupal - Installation and getting started
- jQuery hover
- jQuery hover de-duplication example
- jQuery or CSS?
- jQuery performance
- jQuery tests
- Web site hosting
- Westpac Web site still broken after two years and ten months
- Wordpress wins another CMS survey
- Drupal
Session Variable
Submitted by Peter on Thu, 2010-05-20 10:31
Session Variable is a Drupal module to help you use session variables the same way you use Drupal variables. When session variables are appropriate, they are faster. Read the What, When, Where, and Why of session variables.
What
Drupal is based on PHP code. One of the first things you learn about PHP is variables including arrays. Arrays are lists of variables and, as arrays are variables, an entry in an array can be an array. The session variable is an array that is passed automatically from one page to another when you browse a web site. The session variable can carry any data you like from page to page.
Session Variable supplies easy functions for storing data in the session. If you write Drupal code, you know how to use the Drupal variable functions, variable_set, variable_get, and variable_delete. Session Variable supplies an equivalent session_variable_set, session_variable_get, and session_variable_delete plus additional functions for testing your code.
When
A session is created by Drupal after Drupal processes part of the Drupal code. Sessions require HTML cookie headers and once the HTML headers are set, you cannot redirect a page. Drupal performs page redirects first then starts a session. Most of your code runs after the session is set. An initialisation function might run before the session is set. You can use session_variable_check_session() to test for an active session. See session_variable.test.txt for an example.
A session is created when a visitor arrives at your site and is deleted when some time has expired. Some content management systems create a new session when a user logs. Many content management systems delete the current session when a user logs out. You can pass session variables for one user from page to page but you may lose the variables if the user logs in or logs out or goes out to lunch.
A session is unique to a visitor. If you want information accessed by all visitors, use the Drupal variable functions.
Where
You can use Session Variable functions anywhere in your module code and in content with an input format that includes PHP code. If you write startup
code, code designed to run very early in the Drupal startup sequence, the session may not exist. There is a function to test for a session.
Why
The Drupal variable system has to store code in in the database and it stores each variable as a separate row, creating overheads. Sessions already exist. There is one session read when you visit a page and one session write after the code completes the processing of the page. You can add a variable to the session without increasing the number of reads or writes. Your code is more efficient.
Some data is too large for the way some systems handle sessions and the same data would be too large for the Drupal variable system. If your data fits in a Drupal variable then it can fit in a session variable with less overhead.
Some data is only ever temporary. As an example, a visitor to your site might ask for an exchange rate from one currency to another. You look up the exchange rate using a Web service. There are 200 countries in the world with most having unique currencies. You could have 200 x 200 exchange rates, a total of 40,000. Your Web service might provide new exchange rates every 5 minutes. Do you want to store 40,000 exchange rates in a database and update them every 5 minutes? Not is you have only a few thousand users of your Web site. You could write your code to get the current exchange rate for that user and store it in the session for 5 minutes.
In the exchange rate example, you may choose to store a temporary copy of the exchange rate in the Web service providing the exchange rate so the exchange rate can be automatically updated. Web services have a greater overhead than a session variable and you would not want to continually access a Web service if you can replace most of the accesses with a session variable. The ideal would be to receive an exchange rate and an expiry time for the rate. You would store the rate and the expiry time in the session then use the value from the session until it expires.
session_variable.zip
session_variable.zip contains all the files you need to install and test Session Variable. Download session_variable.zip. Expand the .zip file then copy the resultant directory to your sites/all/modules directory. In Drupal, select Administer » Site building » Modules then activate Session Variable. session_variable.tar.gz contains exactly the same files compressed a different way.
session_variable.info
session_variable.info is required by Drupal to identify the module.
session_variable.module
session_variable.module contains the code used by Drupal.
session_variable.test.txt
session_variable.test.txt is included in the download to test the module. After you activate the module, create a Drupal page with a input format of PHP then copy the test text into the page and preview. You will see the result of the test. You do not have to save the page because you always have the test text if you want to test again.
Test the code
Here is a test of session_variable.test.txt copied direct into this page.
name = Session Variable description = Store variables in your session. From PeterMoulding.com. copyright PeterMoulding.com 2007. Free for use with Drupal. version = "6.0-1.0" package = "Pet" core = 6.xSession variable is not installed.
Documentation
Here are the functions provided by Session Variable.
session_variable_set
session_variable_set($module, $name, $value = '')
You put data into a variable using session_variable_set. The format is similar to the Drupal variable_set with the addition of your module name to make your variables unique. The module value could be anything when used outside a module. Code in a page content could use the title of the page.
The value is optional for those occasions when you want to set a quick default value for a string. The value could be an object or any other type of variable. For objects, PHP stores only the data from an object in the session. The class code for the object has to be loaded before you can get the object from the session. PHP can autoload classes if your PHP is set up with the right configuration.
Here is an example of storing variable test for module example.
session_variable_set('example', 'test', 'Some data');
session_variable_get
session_variable_get($module, $name)
You get data from a variable using session_variable_get. The format is similar to the Drupal variable_get with the addition of your module name.
If the value is an object, the class code for the object has to be loaded before you can get the object from the session. See the comments in session_variable_set.
Here is an example of getting variable test for module example.
session_variable_get('example', 'test');
session_variable_delete
session_variable_delete($module, $name)
When you are finished with a variable, delete the variable so the session does not bloat from too many variables. The format is similar to the Drupal variable_delete with the addition of your module name.
This function returns true if the variable exists and false if the variable does not exist.
Here is an example of deleting variable test for module example.
session_variable_delete('example', 'test');
session_variable_is
session_variable_is($module, $name)
There are times when you want to find out if the module is created. You cannot always test for a default value because some variables have wide variety of value values. This function returns true if the variable is in the list of variables for the module and false if the variable does not exist.
Here is an example of checking variable test for module example.
if(session_variable_is('example', 'test'))
session_variable_check_module
session_variable_check_module($module)
Are you using replacing the module name with a value from a title or a similar unknown field? you can use session_variable_check_module to check the validity of the value. This check is not important because the module field is cleaned up before use.
Here is an example of checking variable $title.
if(session_variable_check_module($title))
session_variable_check_session
session_variable_check_session()
Does the session exist at the point where you want to use session variables? In most cases, yes. For page content, always yes. You can add a test for the session when first developing a module. This function returns true when the session exists and false when the session does not exist. Here is an example of checking for a session.
if(session_variable_check_session())
session_variable_safe
session_variable_($name = '')
You will not need this function. It is an internal function using a quick simple method to make a string safe for use as a variable name. You could use it if you want to use a safe name outside of Session Variable. Here is an example of making a string safe for use as a variable name.
$safe_name = session_variable_safe('Example string')









Comments
This article was a real tour
This article was a real tour into the Session Variable and I must admit that I’ve never come across an excellent tutorial like this in a long while!! I just knew that it was a Drupal module which helped you do a lot of things!! But after this write-up about the “what, when, why and where” of the Session Variables, I am definitely going to install it and make use of this wonderful module as I am sure will many others!!
File not found?
I'm trying to download the module, but I get a
Page not found
The requested page "/sites/all/modules/session_variable.zip" could not be found.
Any chance to get this fixed? I'm trying to store session vars here, for anonymous users, but Drupal won't let me ... :/
Fixed
The current file is D6. Just change the numbers in the .info file for D7.