2016-05-31 40 views
0

所以我有這三個子文件夾。該文件夾代表三個用戶。我有這個session.php這將檢查用戶的「ID」,請它將繼續登錄,但如果現在將您重定向到index.php如何將PHP重定向到根目錄index.php

這裏是我的代碼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']; 
?> 

我知道代碼是正確的,我唯一的問題是header(); 例如,當我輸入的位置/ rootFolder /子文件夾/ file.php

它會給我的PHP錯誤未找到對象或文件不存在

+1

你絕對需要證明從'位置/ rootFolder產生錯誤的代碼的一個子集/ SubFolder/file.php',否則你可能會得到比彩票組合更糟糕的答案。 –

+0

[在PHP中獲取webroot]的可能重複(http://stackoverflow.com/questions/2424606/get-webroot-in-php) – fyrye

+0

只需在'index.php'之前添加'/'。 'header(「Location:/index.php」)' – roullie

回答

0

假設你想從文件夾中移動到文件夾外索引頁,然後在這種情況下,你可以使用:

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

仍然不能正常工作 –

+0

你能否詳細說明......你想從哪裏去哪裏 –

+0

例如我輸入localhost/RootFolder/SubFolder/file.php而不是localhost/Rootfolder/index.php這是我得到的文件不存在 –

0

你應該只需要到您的前綴相對URI一個/像這樣: header('Location: /index.php');


更新

被重定向到的網址相對腳本則不然,它是相對於當前用戶的URL。 所以如果session.php/system/functions/session.php

和當前的URL是http:://example.com/user/subfolder/file.php

  • header('Location: index.php');會重定向到http:://example.com/user/subfolder/index.php

  • header('Location: ../index.php');會重定向到http:://example.com/user/index.php

  • header('Location: ../../index.php');會重定向到http:://example.com/index.php

  • header('Location: /index.php');會重定向到http:://example.com/index.php

  • header('Location: /');會重定向到http:://example.com/

因此,爲了得到location/rootFolder/SubFolder/file.php如果能在http://example.com/SubFolder/file.php

發現你會使用header('Location: /SubFolder/file.php');

這是假設rootFolder是你的網絡服務器DOCUMENT_ROOT如可在$_SERVER['DOCUMENT_ROOT'];

+0

仍然不起作用 –

+0

@AdrianPascuaVivsTV更新了相關路徑的工作方式。 – fyrye

+0

@AdrianPascuaVivsTV如果你的意思'location'實際上是'localhost'在你的問題中,請更新你的問題來閱讀'localhost',因爲我們認爲它是一個物理文件位置。 – fyrye

0

被看作是你的指數是在父目錄使用:header("Location: ../index.php");

如果您index.php是在根目錄:header("Location: /index.php");

使用/可能會引發錯誤更好的方法:header("Location: http://{$_SERVER['HTTP_HOST']}/index.php");

+0

仍然無法正常工作 –

+0

您得到的錯誤是什麼? –

+0

在URL中我輸入localhost/rootfolder/subfolder/file.php –

0

如果您的當前頁面和下一頁出現在同一個目錄中意味着不需要任何/斜槓否則,你可以通過../file.php回去單個目錄或返回兩個目錄來通過../../file.php像下面這樣

0

用途:

header("location: /"); 
相關問題