kamal
kamal

Reputation: 205

Redirecting back to index page

I have a home page called index.php.. And a registration page called registration.php.

The index.php has some logic in it that needs to be executed inorder for the site to work properly.

So lets say the url is http://www.testwebsite.com

and the registration url is: So lets say the url is http://www.testwebsite.com/registration.php

When a user just immediately navigates to So lets say the url is http://www.testwebsite.com/registration.php the logic in index.php gets bypassed and is not executed and causes problems.

So question now..

Is there a way for me to redirect them to the index.php page if they ever go to the registration.php link directly? So the code has to check and see if the index.php webpage was hit first, if it was not hit then it should redirect to index.php. But, if the index.php site was already hit then it should just continue and not redirect back to index.php.

Upvotes: 0

Views: 146

Answers (4)

Troy Corbin
Troy Corbin

Reputation: 51

$_SERVER['HTTP_REFERER'] can be referenced to determine what the previous page was. Example:

if( isset( $_SERVER['HTTP_REFERER'] ) ){
    if( ! strpos( $_SERVER['HTTP_REFERER'], 'testwebsite.com' ) ){
        header( 'Location: http://www.testwebsite.com' );
        exit();
    }
}

Note that you can not always trust this variable:

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

From source: http://php.net/manual/en/reserved.variables.server.php

Upvotes: 0

When "index.php" is executed you can create a session variable, like this:

<?php

session_start();
$_SESSION[ "index" ] = TRUE;  // CREATE SESSION VARIABLE.

?>

Now, in "registration.php" you check for the session variable:

<?php

session_start();
if ( ! isSet( $_SESSION[ "index" ] ) ) // IF SESSION VARIABLE DOES NOT EXIST.
   header( "Location: index.php" ); // JUMP TO INDEX.

?>

Both of these codes must be at the top of their corresponding pages.

Upvotes: 1

m.edmondson
m.edmondson

Reputation: 30922

You set a cookie

I can see you're using PHP, that would mean setting a session variable indicating that index.php was hit. This can then be checked on registration.php, performing a redirect if not set.

Take a look at the docs over here, but it's essentially a case of

!isset($_SESSION['was_index_hit'])

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167212

You can use something like:

<?php
    if (condition when you wanna go to index)
        header("Location: ./index.php");
?>

Upvotes: 0

Related Questions