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
https://forums.phpfreaks.com/topic/134373-php-user-authentication-help-please/
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.

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 =).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.