2011-08-04 90 views
0

我有index.php文件,其中包含所有樣式表,js等文件,我只使用require一次更改index.php的內容區域中的文件。問題是,當用戶請求時一些其他頁面會話丟失....和會話變量未定義...這是我的index.php ...而我訪問main.php,我得到會話變量未定義的錯誤... ---- index PHP文件-----路由URL丟失時會話丟失的值

<?php session_start();?> 
<?php require_once("cc_includes/sessions.php"); ?> 
<?php require_once('cc_includes/functions.php'); ?> 
<?php require_once("cc_includes/sanitize.php"); ?> 
<?php require_once('cc_includes/route.php'); ?> 
<?php require_once("cc_includes/mydb.php"); ?> 
<?php 
    if($request_uri_header!='') 
    { 
     require_once($request_uri_header); 
    } 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <?php require_once("cc_includes/default_files.php");?> 
</head> 
<title><?php echo $the_title;?></title> 
<body id="page1"> 
<!-- header --> 
    <div class="bg"> 
     <section> 
      <?php require_once("cc_includes/message.php"); ?> 
      <div class="main"> 
       <header> 
        <?php require_once("cc_includes/logo.php"); ?> 
        <?php require_once("cc_includes/navigation.php");?> 
        <?php require_once("cc_includes/slider.php"); ?> 
       </header> 
       <section id="content"> 
        <div class="padding"> 
         <?php require_once("cc_includes/boxes.php"); ?> 
         <div class="wrapper"> 
          <div class="col-3"> 
           <div class="indent"> 
            <?php 
             if($request_uri!='') 
             { 
              require_once($request_uri); 
             } 
            ?> 
           </div> 
          </div> 
         </div> 
        </div> 
       </section> 
       <?php require_once("cc_includes/footer.php"); 
       require_once("cc_includes/end_scripts.php"); 
       ?> 
      </div> 
     </section> 
    </div> 
</body> 
</html> 

----- sessions.php文件------

$session_user=false; 
    $session_message=false; 
    if(!isset($_SESSION) || !isset($_SESSION['user'])) 
    { 
     $session_user=array(
       's_name'=>'', 
       's_gender'=>'', 
       'college_id'=>'', 
       's_joining'=>'Dynamic', 
       's_department'=>'Dynamic', 
       's_location'=>'', 
       's_dob'=>'', 
       's_approved'=>0 
      ); 
     $_SESSION['user']=serialize($session_user); 

    } 
    else 
    { 
     //print_r(unserialize($_SESSION['user'])); 
     //exit; 
     $session_user=unserialize($_SESSION['user']); 
    } 

-----route.ph P檔--------

if(isset($_GET['url'])) 
    { 
     $total_request=explode('/',$_GET['url']); 
     if(count($total_request)>1) 
     { 
      $_GET['url']=$total_request[0]; 
      array_shift($total_request); 
      $_GET['action']=$total_request[0]; 
      array_shift($total_request); 
      foreach($total_request as $key=>$value) 
      { 
       $_GET['param'.$key]=$value; 
      } 
      unset($total_request); 
     } 
     if($session_user['s_approved']!=0) 
     { 
      if($_GET['url']=='' || $_GET['url']=='index.php') 
      { 
       header("location: main.php"); 
      } 
      if(!is_file($_GET['url'])) 
      { 
       set_error_message("No Such Location Exits!"); 
       header("location: main.php"); 
      } 
      $request_uri=$_GET['url']; 
      $request_uri_header="headers/".str_replace('.php','.h',$request_uri); 

     } 
     else 
     { 
      $request_uri="users/login.php"; 
      $request_uri_header=str_replace('.php','.h',$request_uri); 
      if($_GET['url']!='' && $_GET['url']!='index.php') 
      { 
       if($_GET['url']=='services.php' || $_GET['url']=='register.php') 
       { 
        $request_uri=$_GET['url']; 
        $request_uri_header="headers/".str_replace('.php','.h',$request_uri); 
       } 
       else 
       { 
        if(is_file($_GET['url'])) 
        { 
         set_error_message("You need to Login Before Accessing Other Site Area!"); 
        } 
        else 
        { 
         set_error_message("No Such Location Exits!"); 
        } 
        header("location: index.php"); 
       } 
      } 
     } 
     if(!is_file($request_uri_header)) 
     { 
      $request_uri_header=''; 
     } 
    } 
    else 
    { 
     $request_uri="users/login.php"; 
     $request_uri_header=str_replace('.php','.h',$request_uri); 
    } 

---- main.h文件-----

if(!registered_user() && !admin_user()) 
    { 
     set_error_message("Please Login To View The Page!",2); 
     header("Location: index.php"); 
    } 
    set_title("College Connections | Home"); 
    view_boxes(false); 
    view_slider(FALSE); 

----- main.php文件---- -

<?php 
    // if(!isset($session_user)) 
    //{ 
     //echo $session_user['s_name']; 
     //exit; 
    //} 
    //print_r($session_user); 
    //exit; 
    echo"<h1 class=\"profile\">".$session_user['s_name']."</h1><h1 class=\"blue_profile\">'s Profile</h1><a href=\"dashboard.php\" style='float:right;margin-right:30px;margin-top:20px;'>College Dashboard</a><br /></br /><br />"; 
    echo"<hr>"; 
    echo"<div class=\"prof_image\">"; 
    $c_id=$session_user['college_id']; 
    $image=mysql_query("select path from img_upload where username=\"$c_id\" limit 1",$connection); 
    if(!$image) 
    { 
     die("database query failed".mysql_error()); 
    } 
    echo mysql_error($connection); 
?> 

回答

0

您還沒有叫比index.php文件以外的任何文件session_start()所以當其他頁面加載的會議正在喪失

+0

但文件都包含在index.php文件的調用。 ....所以爲什麼我需要寫會話開始在每個文件....和網站是相當實用的時候它是活的,但是當我運行它在本地主機上我得到這個錯誤 – Birju

+0

已經熟悉if($ request_uri!='') { require_once($ request_uri); }在index.php中,它在route.php – Birju

+0

中操作當你寫「當用戶請求其他頁面」時,這導致我相信你指的是index.php文件中沒有包含的頁面,另外route.php看起來好像將用戶重定向到main.php,而在調用'header(「location:main.php」)時沒有將內容加載到index.php中,嘗試手動設置會話,如果這樣工作,那麼會話創建需要改進因爲如果他們工作的話,這意味着會議正在被重寫而沒有數據。對不起,但我必須現在運行,所以我今天無法幫助,但我希望這有助於:) – Ryan