Jump to content

PHP - User authentication (HELP PLEASE)


paulman888888

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.