banner



How To Upload Two Sessions

How to Apply Sessions to Rails User Data Using PHP

PHP

In this article, we show how to employ sessions to rails user data using PHP.

This is actually a powerful concept because sessions data tin can stored on the spider web server. Therefore, if a user clicks and goes to some other page, the server even so has the information most the user.

Many sites use this concept of sessions. Pretty much any site that has a login organization where a user logs in will use sessions. Once a user is logged in, a session is begun. If the user goes to another folio or fifty-fifty gets off the site to another and comes back, their data is still available, so they can pick upward correct where they left off.

Shopping sites use this all the time. You lot log into your business relationship and no matter what page y'all go to on the site, you lot're still logged into your account and the website knows who you are.

And so how practise sessions work and how tin we implement them on a website with PHP?

Class To Track You Through Sessions

Earlier we go get into the lawmaking for this tutorial, enter your offset name into the text box above.

This form will illustrate sessions for you lot.

You will see how y'all go from page to page, yet each folio knows your get-go proper noun. This is washed through sessions.

We will now go over how to do this beneath in code.

HTML Code

So let's say just like to a higher place nosotros have an HTML form that gets data from a user, such every bit above with a text box asking for the user'due south commencement name.

The HTML code for this is shown below.

The <course></course> tags create an HTML form.

We set the action attribute of the form to userpage.php. This is because this is the PHP folio we desire to transfer the user to when the user clicks on the Submit button.

The method is set to Mail because we're posting information technology to the actual page, not the URL, which is what the Become method does.

We then take a standard HTML text box that asks for the user'due south name.

This is followed by a submit push button, which allows a form to be submitted.

PHP Code

And so now nosotros go to the outset PHP lawmaking that is used one time the user submits the form.

The contents of the PHP file, userpage.php, is shown below.

So the kickoff thing that nosotros do is call the function, session_start().

Session_start() is 1 of the crucial of sessions. Without it, sessions could non work.

In order for you lot to start a session that is able to track a user or user information, you must start a session. This is what the session_start() office does.

This code must be before whatever other lawmaking on a page, or else it will not piece of work. Therefore, when you use the session_start() role, do not include whatever other code before this of whatsoever kind, including HTML lawmaking. You lot volition get the error, "Headers already sent". You must put this first in your lawmaking. If you nevertheless go the error, "Headers already sent" and you are non including any white infinite or code of any kind before the session_start() function, then save your PHP file with the ANSI encoding. More than likely, your PHP file is saved either with Unicode, Unicode Big Endian, or UTF-viii encoding. These type of encodings add invisible characters before that causes PHP to throw errors.

And so after this session_start part, nosotros retrieve the data that the user entered into the form. Since the name of the HTML text box was name_entered, we use the superglobal array $_POST to retrieve the information entered into the this text box; nosotros assign the value of this information to the variable $proper name.

The adjacent line of code is very important. We create a sessions variable called proper noun that is ready equal to what the user has entered into the text box. This way, $_SESSION['name'], is equal to the name that the user has entered. This is how nosotros will track the user or the user's information from page to page.

Considering session variables are saved on the server, even if a user goes to some other page, this sessions variable is yet stored. Therefore, we can track users from page to page to page. This, again, is how many sites that have users logged in allow a user to see they are yet logged in fifty-fifty when going from dissimilar page to unlike page.

So on this userpage.php file, nosotros transfer the user to some other page to show that we can rails the user (the user's information) through session variables.

This page is anotheruserpage.php, which we bear witness the contents of below.

The biggest thing to note near this page is it has another session_start() function.

In fact, every page that tracks a user through sessions must have the session_start() function at the acme of the PHP code.

The session_start() function is used for when a session is first begun and to continue a current session for a user. It might seem a little weird to have a session called session_start when you're continuing an already started session, but look at it like this, information technology starts a session for each folio, and so that the session is continued on other pages. That's the way I run across it, at least.

So, once again, for each page that tracks user data through sessions, there must be a session_start() part on that folio. And so if yous accept a very interactive site where you desire a user to be logged in no matter what page they are visiting on your website, that means you will accept a session_start function on every folio on your website.

And then later this session_start() part, we output a statement that we nonetheless know who the user is even though this is another completely separate page. And we know this because of the session variable, $_SESSION['proper noun'], that we created. Once a session variable is created, which we did on the userpage.php file, and we use the session_start() function on each page after that, the server nonetheless has the value stored in this session variable. Then if we phone call it, it still has the value. And then any you entered into the HTML grade is all the same contained even later on at present 2 PHP files.

Afterward this folio, we demonstrate i last page tracking the user data through sessions. Why not?

The page we transfer the user to now is lastuserpage.php.

The contents of the lastuserpage.php file is shown beneath.

And so, again, as explained, we must include the session_start() function. This is needed on every PHP folio that you lot want to track session information, either to create a session or maintain a current session.

We output the user's name (stored in the $_SESSION['proper name'] variable) and say this is just 1 last page to show you the session data transfers over.

And this is really all that is required of the basics of working with sessions.

If you lot want to terminate a session, a session normally ends when a user closes the browser or even sometimes when a user goes to a website. Only if you want to definitively end a session with PHP, then you tin can utilise the session_destroy() role. This terminates a user'south session so that the user is no longer being tracked.

In this article, we just showed one example of a session variable but you can store as many session variables as you want. In our grade, we only tracked the user's start name. However, we tin track ane of hundreds of variables if we want, including final proper name, email address, phone number, etc. We can track anything. This tin be information that is entered into an HTML grade, such as yous did. Or yous tin have a user log in on a site and have data from by history such as the long grade that have to complete when they're initial registering on the site or their shopping history based on past purchases. You may have this data stored in a database and then pull it from the database and shop it in session variables for the duration of the session. So there's many dynamic ways of doing information technology but sessions is a powerful programming concept to acquire and many sites use it to track users from page to page.

Related Resources

Source: http://learningaboutelectronics.com/How-to-use-sessions-to-track-user-data-using-PHP.php

Posted by: carrollcieved.blogspot.com

0 Response to "How To Upload Two Sessions"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel