The list of all PHP and Mysql posts

Redirect the user to Login page , SESSIONs

Index page (first page) in a forum just contains a bunch of links that redirect the user to different forum topics but if we want just registered users can see the posts inside the topics so we should add some php codes  for this purpose.
To add authority property to the website we use SESSION.

SESSION :
In the simplest language , SESSION is an array (variable) that is defined right after a page loaded and its value remains valid untill the user shut that internet page. The name of this array is : $_SESSION["element"].

You can put any parameter inside of this array and use it to transfer data from one page to an other page of your own website.

To begin work with SESSIONs you should know two points:

1. You should start SESSION before use it.You can start SESSION by the following order:

Code:
session_start();

2. SESSION is a kind of header for the web page , and all of the headers should be sent before any output on the page.

for example This code:

Any HTML Code i.e.
<b> name </b>
<?php
session_start();
$_SESSION["auth_usr"] = "a";
?>


Will cause an error , because we have some output before session. Headers should be the first lines of the codes in a file.



MAKE THE USERS TO SIGN up

By the following codes if your user wants to see a page the server will redirect him/her to a login page and after login or signup , it will redirect the user to the requested page.

Put these codes in a file and rename the file to "auth.inc" (the suffix can be anything).

code:
<?php
session_start();
//start the session
error_reporting( 0); // this order prevent server to make any error (remove it when you are programing your website to see the errors)
if(!isset($_SESSION["auth_usr"])){  // "auth_usr" is an element name of session array that you will define it in login file to show that the user has logged in correctly , here You check if this parameter has been assigned or not. "isset" checks if  $_SESSION has any elements with the name of  "auth_usr". $_SESSION["d"] = $_SERVER["REQUEST_URI"]; // $_SERVER is a global array that is produced automatically by the server and you just use one of its elements here : $_SERVER["REQUEST_URI"]. This element contains the address of the current page. Here you put the address of the current page in a session to redirect the user to this page again after login.
 header("Location: login.php"); // this order (header) changes the address bar to the Location , Here it redirects the user to Login page.
 exit; // this order stops all of the process in the current page so the browser can go to the Location.
}
?>


Now add the following codes to the top of all the files that need authority.

code:
<?php
include "auth.inc";
// "include" order adds the total contain of the file in front of it (auth.inc) to the top of the current file.
?>

Infact we add all the codes above to all files (that need authority) just by "include" order.
If you add this code to your files , users are redirected to "login.php" to register or login before they can see their requested page.
To see how to make login page you can go to the post about login page in the current blog.

No comments:

Post a Comment