So the next article in my “Back to Basics” series is all around creating PHP sessions, however with a slice of difference by utilizing ajax and jQuery to seamlessly create them.
PHP Sessions
PHP Sessions are used to store information about your users, which you can carry from page to page without doing much at all. You can use them to manage logged in users quite efficiently by checking if a session exists for that user or not. To create a session you do the following:
session_start(); $_SESSION['username'] = "steve"; $_SESSION['uid'] = "123456";
As you can see, pretty simple, storing the data as variables. So, let’s set up a PHP script called login.php with the following code:
<?php
if(isset($_POST['username']))
{
session_start();
$_SESSION['username'] = $_POST['username'];
echo "1";
} else {
echo "0";
}
?>
What this code will do is simple, retrieve a POST variable called “username” and create a new SESSION variable with the same name. As I said, this is a basics tutorial, so what we’re also doing here is returning a value of “1″ to say that the session was successfully created, or “0″ if something went wrong. This will be used by our jQuery…
The HTML
Let’s just create a basic HTML page with an input box and a button. We’ll also setup a response DIV container for our feedback after setting up the session:
<input id="username" name="username" type="text" /> <input id="submit" name="submit" type="submit" value="Create Session" /> <div id="container"></div>
The jQuery
Lastly, the jQuery that needs to take the form data and send it to our PHP script via ajax and also, return a response:
$(document).ready(function() {
$("#submit").click(function(){
$.post("login.php", {username: $("#username").val()}, function(data){
if(data == "1")
{
$("#container").html("<b>SESSION Setup Successfully!</b>");
} else {
$("#container").html("<b>Something Broke! HELP!</b>");
}
});
});
});
So this code firstly wires up a click event onto the “submit” button. It then makes a jQuery POST ajax call off to your login.php file, sending the value of the input box as a username. The PHP file then does the rest and returns a result of 1 or 0, which our jQuery then neatly updates your HTML with.
Conclusion
So why is this cool? Well, ultimately, it’s not…. but what this does demonstrate is jQuery’s built in ajax ability to send and receive pieces of data to and from scripts. This simple demo shows you how you can now build in login functionality into your app without leaving the page. You could spice it up a bit by returning JSON objects or XML if you wish instead of simple zeros and ones, but you get the idea…
To access your newly created PHP sessions variable, simply call it like any other variable after “session_start();” e.g. $_SERVER['username'];
Download
I’ve compiled this simple tutorial into a couple of scripts so that you can download it and test it out. Any problems, shout in the comments!

Tools to help you learn…
|
|
|
|
Similar Articles:
Tutorial: From PHP to XML to jQuery and Ajax
From MySQL to jQuery, via PHP, XML & Ajax
jQuery - PHP & Ajax with the Twitter API
CSS Positioning Techniques for Nested DIVs
Twitter API calls in the browser with JSON and jQuery












Your notes come at an excellent time for me. Thanks for this.
really nice tut thx very much ¡¡¡
Only question I have is a pretty minor one, so don’t address it if you don’t want. How can I submit the form by hitting the enter button instead of having to click on the submit button?
I think this might be exactly what I’ve been looking for!!
I could have the form action post variables to process a buy it now paypal button transaction and use the jquery to hook a click event to handle some session vars for my site that don’t need sending through the paypal process
or am I way off?
hmmmmm
doesnt seem to work, firebug couldnt find the jquery src so i fixed that but still
all i get is the “something broke” error