Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Slightly reformatted the sample to avoid long lines, etc. Removed meta information (this belongs in comments).
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134

There are multiple ways of doing this, but if you’d prefer php, I’d recommend the use of the header() function.

Basically

$your_target_url = "www.example.com/index.php";
header("Location : $your_target_url");
exit();

If you want to kick it up a notch, it’s bestbest to use it in functions, that. That way, you are able to add authentications and other checking elemnts in it.

Let’s try with by checking the user’s level.

So,suppose suppose you have stored the user’s authority level in a session called u_auth.

In the function.php

<?php
function authRedirect($get_auth_level, $required_level, $if_fail_link = "www.example.com/index.php"){
 if($get_auth_level != $required_level){
 header(location : $if_fail_link);
 return false;
 exit();
 }else{
 return true;
 }
 }
 . . . .

You’ll then call the function for every page that you want to authenticate.

Like in page.php or any other page.

<?php
// page.php
require "function.php"
authRedirect($_SESSION[‘u_auth’], 5); // redirectsRedirects to www.example.com/index.php if the // user isn’t authauthentication level 5
authRedirect($_SESSION[‘u_auth’], 45); // redirectsRedirects to www.example.com/index.php if the // user isn’t authauthentication level 4
authRedirect($_SESSION[‘u_auth’], 2, "www.someotherplace.com/somepage.php"4); // redirectsRedirects to www.someotherplace.com/somepage.php if the // user isn’t authauthentication level 2
 authRedirect($_SESSION[‘u_auth’], 2, "www.someotherplace.com/somepage.php");
. . . .

I hope you’ll find some of the content useful

References;

There are multiple ways of doing this, but if you’d prefer php, I’d recommend the use of the header() function.

Basically

$your_target_url = "www.example.com/index.php";
header("Location : $your_target_url");
exit();

If you want to kick it up a notch, it’s best to use it in functions, that way, you are able to add authentications and other checking elemnts in it.

Let’s try with by checking the user’s level.

So,suppose you have stored the user’s authority level in a session called u_auth.

In the function.php

<?php
function authRedirect($get_auth_level, $required_level, $if_fail_link = "www.example.com/index.php"){
 if($get_auth_level != $required_level){
 header(location : $if_fail_link);
 return false;
 exit();
 }else{
 return true;
 }
 }
 . . . 

You’ll then call the function for every page that you want to authenticate.

Like in page.php or any other page.

<?php
// page.php
require "function.php"
authRedirect($_SESSION[‘u_auth’], 5); // redirects to www.example.com/index.php if the user isn’t auth level 5
authRedirect($_SESSION[‘u_auth’], 4); // redirects to www.example.com/index.php if the user isn’t auth level 4
authRedirect($_SESSION[‘u_auth’], 2, "www.someotherplace.com/somepage.php"); // redirects to www.someotherplace.com/somepage.php if the user isn’t auth level 2
. . . 

I hope you’ll find some of the content useful

References;

There are multiple ways of doing this, but if you’d prefer php, I’d recommend the use of the header() function.

Basically

$your_target_url = "www.example.com/index.php";
header("Location : $your_target_url");
exit();

If you want to kick it up a notch, it’s best to use it in functions. That way, you are able to add authentications and other checking elemnts in it.

Let’s try with by checking the user’s level.

So, suppose you have stored the user’s authority level in a session called u_auth.

In the function.php

<?php
function authRedirect($get_auth_level, $required_level, $if_fail_link = "www.example.com/index.php"){
 if($get_auth_level != $required_level){
 header(location : $if_fail_link);
 return false;
 exit();
 }else{
 return true;
 }
 }
 . . .

You’ll then call the function for every page that you want to authenticate.

Like in page.php or any other page.

<?php
// page.php
require "function.php"
 // Redirects to www.example.com/index.php if the // user isn’t authentication level 5
authRedirect($_SESSION[‘u_auth’], 5); // Redirects to www.example.com/index.php if the // user isn’t authentication level 4
authRedirect($_SESSION[‘u_auth’], 4); // Redirects to www.someotherplace.com/somepage.php if the // user isn’t authentication level 2
 authRedirect($_SESSION[‘u_auth’], 2, "www.someotherplace.com/somepage.php");
 . . .

References;

Source Link
Pyr James
  • 731
  • 1
  • 6
  • 10

There are multiple ways of doing this, but if you’d prefer php, I’d recommend the use of the header() function.

Basically

$your_target_url = "www.example.com/index.php";
header("Location : $your_target_url");
exit();

If you want to kick it up a notch, it’s best to use it in functions, that way, you are able to add authentications and other checking elemnts in it.

Let’s try with by checking the user’s level.

So,suppose you have stored the user’s authority level in a session called u_auth.

In the function.php

<?php
function authRedirect($get_auth_level, $required_level, $if_fail_link = "www.example.com/index.php"){
 if($get_auth_level != $required_level){
 header(location : $if_fail_link);
 return false;
 exit();
 }else{
 return true;
 }
 }
 . . . 

You’ll then call the function for every page that you want to authenticate.

Like in page.php or any other page.

<?php
// page.php
require "function.php"
authRedirect($_SESSION[‘u_auth’], 5); // redirects to www.example.com/index.php if the user isn’t auth level 5
authRedirect($_SESSION[‘u_auth’], 4); // redirects to www.example.com/index.php if the user isn’t auth level 4
authRedirect($_SESSION[‘u_auth’], 2, "www.someotherplace.com/somepage.php"); // redirects to www.someotherplace.com/somepage.php if the user isn’t auth level 2
. . . 

I hope you’ll find some of the content useful

References;

lang-php

AltStyle によって変換されたページ (->オリジナル) /