2012-02-25 55 views
0

具有在本地主機會議的問題,我的問題是,當它重定向到新的崗位頁面會話實際上並不工作,它的反應是,你應該先登錄會議不能正常工作

登錄代碼

<?php 
include '../Class/db.class.php'; 
include '../Class/validation.class.php'; 
@session_start(); 
if(!$_SESSION['user']){ 
    $link=$_GET['dir']; 
    if($link){ 
echo <<<EOF 
<html> <head><title>ورود</title></head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<form method='post' action='index.php?dir=$link'> 
    Username: <input type='text' name='user'><br /> 
password<input type='password' name='pass'><br /> 
    <input type='hidden' name='hidd' value=1> 
    <input type='submit' value='login'><br /> 
</form> 
EOF; 
    } 
else { 
    echo <<<EOF 
<html> <head><title>login</title></head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<form method='post' action='index.php'> 
    username: <input type='text' name='user'><br /> 
password: <input type='password' name='pass'><br /> 
    <input type='hidden' name='hidd' value=1> 
    <input type='submit' value='login'><br /> 
</form> 
EOF; 
    } 
} 
$db=new db("localhost", "root", "", "blog"); 
$con=$db->connect(); 
$logout=$_GET['logout']; 
$user=$_POST['user']; 
$pass=$_POST['pass']; 
$hidden=$_POST['hidd']; 
if($hidden){ 
if(!$user){ 
    echo "username field can't left blank"; 
} 
elseif (!$pass) { 
    echo 'password field can't left blank!'; 

} 
else{ 
    $userres=$db->usersearch($user, $pass); 

    if($userres==FALSE){ 
     echo 'username or password is wrong'; 
    } 
else { 
     $_SESSION['user']=$user; 

    } 
} 
} 
if($_SESSION['user']){ 
    $link=$_GET['dir']; 


echo "you logged in ".$_SESSION['user']; 
if($link=="new") 
    echo '<meta http-equiv="REFRESH" content="0;url=../admin/post/new.php">'; 

} 

if($logout) 
    session_destroy(); 
if($_SESSION['user']) 
echo '<br /><a href="?logout=1">logout</a>'; 

?> 

新的職位代碼

<?php 
@session_start(); 

if($_SESSION['user']=="admin"){ 
    include '../../Class/db.class.php'; 
echo <<<EOF 
<form method=post action="new.php"> 
<br /> subject: 
    <br /> 
    <input type="text" name="title"><br /> 
    post:<br /> 

    <textarea cols=50 rows=10 name="post"></textarea><br /> 
    <input type="submit" value="send"> 

EOF; 
$title=$_POST['title']; 
$post=$_POST['post']; 
$db=new db("localhost", "root", "", "blog"); 
$db->connect(); 
if($title && $post){ 

    $d=$db->post_insert($title, $title, 1); 
    if($d==TRUE) 
      echo "post created"; 
    else 
     echo "an error occurred while sending the post"; 




} 

$dd=$db->read(0, 20); 
for($i=0;$i<20;$i++){ 
    echo $dd['content'][$i]."<br />"; 
} 

} 
else 
{ 
    echo "you should login first"; 
    echo $_SESSION['user']; 
} 
/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
?> 

請幫幫我!

+4

首先你能告訴我你爲什麼使用@session_start(); @將壓制任何警告等等所以我要做的第一件事就是調用@session_start(); as session_start(); – 2012-02-25 06:30:21

+0

問題解決了謝謝 – hatef 2012-02-25 06:41:37

回答

0

使用@符號抑制session_start()的錯誤輸出這一事實表明session_start報告了您將忽略的錯誤。

如果它報告錯誤,您應該查看它生成的錯誤以確定錯誤。

通常,由於標頭已被髮送,session_start失敗。如果腳本在調用session_start之前輸出任何文本,就會發生這種情況。一旦發送了標題,會話就無法啓動,因爲它取決於發送標題。

解決此問題的方法是確保session_start是您的腳本中首先發生的事情。由於session_start是包含在腳本之後的第一件事,因此這意味着在腳本開始時會有空白,或者在您包含的腳本中生成輸出的空格或語句。您需要檢查包含的文件並修復任何空白區域(PHP標記之外的任何內容),並檢查它們是否在session_start發生之前不輸出任何內容。

在旁註中,請勿使用@作爲錯誤抑制。這是編碼相當於清掃地毯下需要解決的潛在嚴重問題。