Adrian Pascua VivsTV
Adrian Pascua VivsTV

Reputation: 37

how to PHP redirect to the root index.php

So I have this three sub folders. That folders stands for the three users. I have this session.php this will check if the users 'id' is available it will continue to login, but if now it will redirect you to index.php

Here is my code to session.php

<?php
//Start session
session_start();
//Check whether the session variable SESS_MEMBER_ID is present or not
if (!isset($_SESSION['username']) || (trim($_SESSION['username']) == '')) {
    header("Location: index.php");
    exit();
}
$session_id=$_SESSION['username'];
?>

I know the code is correct and my only problem is the header(); For example, when I type location/rootFolder/SubFolder/file.php

It will give me PHP error Object not found or the file is not existing

Upvotes: 1

Views: 2729

Answers (5)

Will B.
Will B.

Reputation: 18416

You should just need to prefix your relative URI with a / like so: header('Location: /index.php');


Updated

The URL that is redirected to is not relative to the script, it is relative to the current user's URL. So if session.php is in /system/functions/session.php

and the current URL is http:://example.com/user/subfolder/file.php

  • header('Location: index.php'); will redirect to http:://example.com/user/subfolder/index.php

  • header('Location: ../index.php'); will redirect to http:://example.com/user/index.php

  • header('Location: ../../index.php'); will redirect to http:://example.com/index.php

  • header('Location: /index.php'); will redirect to http:://example.com/index.php

  • header('Location: /'); will redirect to http:://example.com/

So in order to get to location/rootFolder/SubFolder/file.php if it can be found at http://example.com/SubFolder/file.php

You would use header('Location: /SubFolder/file.php');

This is assuming rootFolder is your webserver DOCUMENT_ROOT as can be viewed in $_SERVER['DOCUMENT_ROOT'];

Upvotes: 1

Apoorv
Apoorv

Reputation: 231

Use below:

header("location: /");

Upvotes: 1

Ani Menon
Ani Menon

Reputation: 28219

As your index is in parent dir use: header("Location: ../index.php");

If your index.php is in root dir: header("Location: /index.php");

Using / may throw errors so better way: header("Location: http://{$_SERVER['HTTP_HOST']}/index.php");

Upvotes: 0

JYoThI
JYoThI

Reputation: 12085

if your current page and next page present in same directory means no need any / slashes otherwise you can go back single directory by ../file.php or go back two directory by ../../file.php like this

Upvotes: 0

aarju mishra
aarju mishra

Reputation: 710

Suppose you want to move from inside folder to outside folder index page then in that case you can use:

header("location:../index.php");

Upvotes: 0

Related Questions