2015-07-10 18 views
0

我是PHP新手。我想使用會話與變量。這是我第一次參加會議。我創建了4頁。如何在PHP中使用會話變量

1是session1.php

<?php 
session_start(); 
$_SESSION['UserID']='1'; 
?> 

2.是session.php文件

<?php 

// starts session 
session_start(); 
if($_SESSION['UserID']='1') 
{ 
    header("location: user.php"); 
    } 
    if($_SESSION['UserID']='2') 
{ 
    header("location: mang.php"); 
    } 
?> 

3.user.php

<?php 

echo "This is User page"; 
?> 

4.mang.php

<?php 
echo "This is manager page"; 
?> 

在我的代碼存在的IF條件問題。我的IF條件不起作用。請你們幫忙解決我的問題。

+3

當你說「我的(空白)不工作」時通常會有幫助,如果你解釋它是什麼或不在做什麼;如果發生錯誤,請告訴我們錯誤! –

+0

@DaymonSchroeder下面是我的問題的答案。我從那個答案中解決了我的問題 – sunny

+0

在** IF **中,您必須使用Compare運算符而不是賦值運算符將$ _SESSION ['UserID'] ='2''改爲'$ _SESSION ['UserID'] ==' 2'' – Bruce

回答

2

變化

if($_SESSION['UserID']='1') and if($_SESSION['UserID']='2') 

if($_SESSION['UserID']=='1') and if($_SESSION['UserID']=='2') 

而且在session1.php也已啓動會話,因此沒有必要在session.php

最終輸出再次啓動會話

<?php 

include ("session1.php"); 

if($_SESSION['UserID']=='1') 
{ 
    header("location: user.php"); 
    exit; 
} 
if($_SESSION['UserID']=='2') 
{ 
    header("location: mang.php"); 
    exit; 
}