paulman888888 Posted November 26, 2008 Share Posted November 26, 2008 hi people; I understand PHP and MySql to a certain degree but i never understand sessions. I am looking for a very simple and basic PHP login script. I know i need these pages; login.php <<--To log the user in I can do this logout.php <<--To log the user out I can do this session_any.php <<--To allow anyone to view the page and keep the session going I can't do this session_safe.php <<--To only allow logined in users to the page I can't do this Please can some tell me a site i can go to to learn the basics, or a very simple script allready made that i can learn from! Thankyou All in advance Paul Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted November 26, 2008 Share Posted November 26, 2008 These pages make no sense! session_any.php <<--To allow anyone to view the page and keep the session going I can't do this session_safe.php <<--To only allow logined in users to the page I can't do this Sessions are the easiest thing in the world to use! All you need is to include a file on every page that starts sessions up, lets say a application.inc.php In this document all you need is: <?php session_start(); ?> For your login file i.e login.php include('application.inc.php'); // the user has attempted to login // check the login details against the database // user details ok - set some session variables $_SESSION['name'] = "Joe Bloggs"; $_SESSION['id'] = 123; // redirect the user to the welcome page header("Location:welcome.php"); exit(); Now on the welcome.php page you can display a message to the user. Remember to include the application.inc.php file: include('application.inc.php'); // display welcome message print "Hello ".$_SESSION['name']; To logout all you do is destroy the session variables so logout.php include('application.inc.php'); // destroy all session variables foreach($_SESSION as $key => $val) { unset($_SESSION[$key]); } print "You were successfully logged out"; For pages on your site that require you to be logged in all you need to do is make sure that the session variable you set after a successful login exists. i.e include('application.inc.php'); if(!is_numeric($_SESSION['id'])) { // the user is not logged in - redirect to login.php header("Login.php"); exit(); } Any book on PHP has sections on sessions. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted November 26, 2008 Share Posted November 26, 2008 Try and create a simple login script and we'll help guide you from there. You dont need a "sesson" page. You can type it in on the top of every page BEFORE anything. <?php session_start(); ?> Try to do your own login script and we'll help you from there =). Quote Link to comment Share on other sites More sharing options...
revraz Posted November 26, 2008 Share Posted November 26, 2008 Let's start small. Do you know what a variable is? If you answer yes, then you can understand sessions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.