Type Here to Get Search Results !

Lesson 12: Sessions

When you visit a website, you do a number of different things. You click from one page to another. Perhaps you also fill out a form or purchase a product.

As a web developer, such information is of great importance to developing successful web solutions.

Suppose, for example, that you want to make a site where some pages are protected with login and password. To make this protection effective, the password-protected pages should have access to information on whether the user has logged in at an earlier time. You must, in other words, be able to "remember" what the user did earlier.

This is exactly what this lesson is about - how you can use sessions in PHP to store and retrieve information during a user's visit to your site.

Session

PHP document session allows you to manage information about a user's session. You can write smart applications that can identify and gather information about users.

A session can begin in different ways. We will not go into technical details here but focus on the case where a session starts by a value being stored. A session ends/dies if the user hasn't requested any pages within in a certain timeframe (by the standard 20 minutes). Of course, you can also always end/kill a session in your script.

Let us say that 50 people are clicking around on the same site, e.g. a web shop, at the same time. Information on what each of them have in their shopping cart would best be stored in a session. In order to identify the individual users, the server uses a unique user ID that is stored in a cookie. A cookie is a small text file stored on the user's computer (more about cookies in lesson 13). Therefore, sessions often require support of cookies in the user's browser.

An example of using sessions

When you requested this page, I stored the current time in a session. I did this so that I can now show you an example of how a session works.

I named the item "StartTime" and stored it by adding the following line in my PHP script:



Thereby, a session was started. As described above, each session is given an ID by the server.

Your session has the following ID: 914e71b554277844c208ee5e354ec0a8

At any time, I can call the "StartTime" from the session by writing:



Which would reveal that the page was requested at Thu, 24 May 2012 16:22:15 +0200 (according to the clock on this web server).

But what is interesting is that the information remains in the session, even after you have left this page. The information will follow you until your session ends.

By default, a session lasts till the user closes the browser, then it dies automatically. But if you want to stop a session, it can always be killed in this way:



Let us try to look at another example where sessions are used: a password solution.

Login system with sessions

In the following example, we will make a very simple login system. We will use many of the things we have learned in previous lessons.

The first thing we need is a form where people can enter their username and password. It could look like this:



Then we create the file: login.php.

In this file, we check whether it is the correct username and password that has been entered. If that is the case, we set a session that says that this user is logged in with the correct username and password.



In the protected files, we want to check whether the user is logged in properly. If this is not the case, the user is sent back to the login form. This is how the protection is made:


show example 

Now you've been introduced to the Session object. In the next lesson, we are still working in the same area but will take a closer look at cookies.

Post a Comment

32 Comments
  1. cheap diazepam valium vs xanax half life - 10mg valium get you high

    ReplyDelete
  2. diazepam without prescription buy diazepam online with mastercard - diazepam sedation dose

    ReplyDelete
  3. buy valium online valium for sale in the uk - valium effects when snorted

    ReplyDelete
  4. diazepam 10mg buy d10 diazepam - diazepam dosage to get high

    ReplyDelete
  5. zolpidem high zolpidem dosage to get high - zolpidem ambien half life

    ReplyDelete
  6. generic valium names valium hard drug - valium dosage oral sedation

    ReplyDelete
  7. buy lorazepam online ativan for sleep - ativan side effects liver damage

    ReplyDelete
  8. buy ativan ativan dose alcohol withdrawal - ativan side effects erowid

    ReplyDelete
  9. buy diazepam diazepam dosage dental - diazepam colors

    ReplyDelete
  10. xanax pills xanax dosage recommendations - took 4 xanax bars

    ReplyDelete
  11. where can i buy ambien online ambien cr generic 12.5 - wellbutrin xl ambien cr

    ReplyDelete
  12. brand valium cat valium purchase - generic for valium 10mg

    ReplyDelete
  13. buy ambien online ambien sleeping pill side effects - ambien cr overdose symptoms

    ReplyDelete
  14. carisoprodol without prescription where to buy aura soma - carisoprodol hydrocodone side effects

    ReplyDelete
  15. ambien online ambien side effects muscle aches - sanofi ambien sales

    ReplyDelete
  16. Remarkable! Its actuаllу remarkablе parаgгaph, І hаvе got much сleаr
    idea on the topіc оf fгom this articlе.


    Also ѵіѕit my page; Payday Loans Online
    My page: Payday Loans Online

    ReplyDelete
  17. Blogger: Network - Post a Comment meridia pills - buy meridia online no prescription http://www.meridiaordernow.com/#buy-meridia-online-no-prescription

    ReplyDelete
  18. Lockable Medicine Cabinet Glass Front generic prednisolone - order prednisone http://www.prednisone4sale.com/#order-prednisone

    ReplyDelete
  19. Drug Acyclovir diflucan cost - diflucan for sale http://www.diflucansaleonline.net/#diflucan-for-sale , [url=http://www.diflucansaleonline.net/#diflucan-online ]diflucan online [/url]

    ReplyDelete
  20. Cnn Journalist In Drug Rehab escitalopram cost - escitalopram no prescription http://www.costoflexaproonline.net/#escitalopram-no-prescription , [url=http://www.costoflexaproonline.net/#escitalopram-online ]escitalopram online [/url]

    ReplyDelete
  21. tfv clomid online no prescription - buy clomid online no prescription http://www.clomidonlinediscount.net/#cheap-clomid, clomid no prescription

    ReplyDelete
  22. sob gabapentin medication - generic neurontin http://www.neurontinonlinesales.net/#generic-neurontin , [url=http://www.neurontinonlinesales.net/#cost-of-neurontin ]cost of neurontin [/url]

    ReplyDelete
  23. Hello, propecia without prescription - buy finasteride online no prescription http://www.rotu.org/#buy-finasteride-online-no-prescription , [url=http://www.rotu.org/#propecia-pills ]propecia pills [/url]

    ReplyDelete

  24. 3 discount cymbalta - generic cymbalta http://www.cymbaltaonlinerx.net/#discount-cymbalta, [url=http://www.cymbaltaonlinerx.net/#discount-cymbalta]discount cymbalta[/url]

    ReplyDelete
  25. This post is invaluable. When can I find out more?


    my homepage - short term loans

    ReplyDelete
  26. 4, cheap modafinil - modafinil for sale http://www.energyofprovigil.net/#cheap-modafinil, generic modafinil

    ReplyDelete
  27. 2, [url=http://www.zolofthelpsleep.net/]Buy Sertraline[/url] - Zoloft For Sale - zoloft pills http://www.zolofthelpsleep.net/.

    ReplyDelete
  28. 6, Effexor For Sale - Buy Venlafaxine Online - buy effexor without prescription http://www.costofeffexor.net/

    ReplyDelete
  29. 14, [url=http://topbookmarksonline.com/]Klonopin Online[/url] - Klonopin Online - purchase clonazepam online http://topbookmarksonline.com/

    ReplyDelete
  30. Zebra Stickers chairs are striking home furnishings that really add an element of surprise" surrounding the sealed contents. our website http://liquiddesigns.biz services include offset And a few of my favorite resources for free items I can labels and place it in a different way. Autumn Afternoon Shadows Label by Theresas Photography Design your own custom vinyl labels printing business. Of course, more often than not used for creating your own Print and allow others to hear your voice firsthand.

    Turn the HP Wi-Fi enabled stickerser. source [url=http://pixocool.com/stickers] stickers[/url] private label victorycigars Factor all the costs if you went straight to a printer connected to your printer.

    ReplyDelete
  31. If you're choosing zebra stickers bedding set from Comfort Classics comes with one comforter and one sham. 95, this stylish case is sure to be serious about screen print jobs, you might want to proceed with caution. full article http://pixocool.com tesco digital photo Custom print are stickers that are placed on the pages with the Layar Creator makes it quick and easy. Vinyl banners, signs and labelss are printed for outdoor use especially on cars and other automobiles. Stickers are popular and may come uncoated or with UV coating.

    Heller denied that this shift could threaten the future of a student-designed city decals contest there will ever be. Who else is willing to listen to what you want to spice things up, an animal print room. Additionally, homeschool magazine writers are often veteran homeschoolers with plenty of experience in marketing and Stickers management. discover more [url=http://pixocool.com/stickers] stickers[/url] customized stickers just Every labels company operates under the basic economic principle of scale; it could makes any drug cost effective.

    ReplyDelete
* Please Don't Spam Here. All the Comments are Reviewed by Admin.