2013-01-12 122 views
0

我想重定向的login.php的index.php當$ _SESSION [「用戶」]不爲空(用戶登錄)如何在PHP中使用多個頭與if語句

<?php 
    session_start(); 
    if (isset($_SESSION['user'])){ 
     header ('refresh:3 ; URL:index.php'); 
    } 
?> 

但是當用戶登錄頁面不重定向到的index.php

回答

5

這應該工作:

<?php 
    session_start(); 
    if (isset($_SESSION['user'])){ 
     header('Location: http://www.yoursite.com/'); 
     die(); 
    } 
?> 

如果你想重定向X senconds後的用戶,然後使用

<?php 
     session_start(); 
     if (isset($_SESSION['user'])){ 
      header("refresh:3;url=whatever.php"); 
     } 
    ?> 
+0

太糟糕了,它不會做他想做的事。 :P – vanneto

+0

正確,只是修復了它 – Stefan

+0

我想在3秒內使用刷新,但當我寫頭('刷新:3;位置:index.php');它不工作爲什麼? – shotgunner

1

你做錯了。 Example of how to do it and some more info about the header.

<?php 
session_start(); 
if (isset($_SESSION['user']) 
{ 
    header ('Refresh: 3; url=index.php'); 
    //     ^
} 
?> 

您使用:它應該是一個等號。

+0

當使用'刷新'標題時,你不應該使用'die'。 –

+0

哈哈,對,打敗了整個目的。修復。 :D – vanneto

+0

感謝當我寫地址而不是URL這工作正常。但是當我寫網址這不起作用。 – shotgunner