[英]Page not getting redirected when sessions are not set - PHP
在home.php頁面中,如果未設置會話,我想重定向到索引頁面。 但是它並沒有被重定向。 僅當我登錄到項目時,才會設置會話。 但是,當前我尚未登錄,因此未設置會話。
require_once 'header.php';
if(!isset($_SESSION['uid']) && !isset($_SESSION['uname']))
{
//echo "hai"; // works
header("location:index.php");
exit;
}
echo "hello"; // does not work
在header.php頁面中,我開始像
if(!isset($_SESSION)){
session_start();
}
我做錯了什么? 誰能幫我發現問題?
提前致謝。
通過header
重定向的正確格式是:
header('Location: '.$newURL);
大寫L
您能嘗試一下並告訴我它如何工作嗎?
require_once 'includes/header.php';
if(!isset($_SESSION['US_Id']) && !isset($_SESSION['US_UName']))
{
header("Location: /index.php");
exit;
}
確保刪除文件和header.php中開始標記<?php
和結束標記?>
之后的所有符號(甚至是空格)。
那里沒有任何內容將無法更改標題。
您的問題是格式錯誤,無法使用相對路徑
更改為:
header("Location: http://yoursite.com/index.php");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.