Lets face it the hardest thing with PHP is ... Sessions. No one can ever get these to work first time, if they do they suddenly stop working. So, I am going to simplify to idiots level.
Introduction
A Session is a SUPER GLOBAL VARIABLE. This means that you can enter some data and call it after F5ing the page or after loading 1000 differen't pages. Aslong as you don't overwrite it ofc
This is good for when you want to collect multiple pages of data, good for making someone an account with your site (It can store the users ID, this then gets passed from page to page, which can be used to call data from the database for that ID) etc ...
The Actual Code
to use/create/pass along sessions the code
session_start()
MUST BE AT THE START OF EVERY PAGE. Before any HTML. If not it will just error.
At the top of every page I just put <?php session_start() ?> at the top of every page before everything else, on line 1.
Putting data into session
Pretty easy but alot of people get stuck on it!
If you got your form ...
<html>
<form method=post action=blah.com>
<input type=text name=UserID>
<input type=submit>
</form>
</html>
And your next page is just going to be where you stick your data into the session
<?php session_start()
//Now to create the session which you want to store data in. You can call ID what ever.
$_SESSION['ID'] = $_POST['UserID'];
?>
And finally the test to see if it worked(A third page)
<?php session_start()
print("$_SESSION['ID']");
?>
Easy as ...
You can use the $_SESSION['ID'] in mysql querys and all over your page if you wish. If you are making a site which requires a login you can make sure there is something in the session, otherwise back to home page.
Deleting a session
Usually the server that your hosting on will delete sessions created after a certain amount of time. Lets say if you dont need to use that session anymore and you don't want it interfering with users reusing your site you can use the command
Session_destroy ()
This will delete any sessions. Use for a log out script or if the values of data that were stored in the session are no longer needed ...
I really, really, hope I explained this well and if you have any questions to ask go for it.
I wish I had a stupidly simple guide like this when starting sessions but all I could find was a 10 page + guide ... pfft why read all that when it could be cut down so simply?