Kets
Kets

Reputation: 468

Check if user is on page?

I have a question about php.

I've made a login/register page, where the user can choose right for login and left for register. I've done the left and right with including the login.php and register.php from the root of my map.

The problem is when users type login.php or register.php behind the web address, they get to the actual page with no style etc.

How can I check that the users are on the both.php page and if they try to access the login.php or register.php by typing it manually, automatically redirect them to both.php?

Upvotes: 2

Views: 1175

Answers (1)

Paul
Paul

Reputation: 141927

What you want to do is possible, you can check $_SERVER['REQUEST_URI'] at the top of login.php, like so:

<?php
if(strpos($_SERVER['REQUEST_URI'], '/login.php') !== false){
    header('Location: /both.php');
    exit;
}

You would do the same for register.php, however, redirecting your users from those pages is not the best solution; instead you should move login.php and register.php outside of your webroot. Any file that you don't want the user to have access to should be outside the webroot.

Let's say that your webroot is called 'public_html'. Then create a directory structure like so:

public_html:
 - both.php
includes:
 - login.php
 - register.php

Then in both.php, to include the other two files, do:

require __DIR__ . '/../includes/login.php';
require __DIR__ . '/../includes/register.php';

Since those files are outside the webroot they cannot be viewed by typing a path into the address bar, but both.php can still include them.

Upvotes: 5

Related Questions