2015-01-04 35 views
2

我在我的課堂文件中設置會話消息,如,然後將頁面重定向到header("location:news.php");頁面重定向到news.php,但會話消息未顯示。無法在PHP中顯示會話消息?

的config.php:

<?php 
    session_start(); 
    // DATABASE CONFIURATION GOES HERE 
?> 

news.php:

<?php 
    require_once 'config.php'; 
    require_once 'classes/class.news.php'; 
    $objNews = new News(); 

    if(isset($_POST['submit']) && $_POST['submit'] == 'add') 
    $objNews->add(); 

?> 

<span class='text-warning'><?php echo $_session['op_status']; ?></span> 

class.news.php:

public function add() { 
    if(){ 
    // SOME CODE GOES HERE 
    } else { 
    $_SESSION['op_status'] == 'Already Exist'; 
    } 
} 
+0

你在這兩個文件中使用它之前,你開始你的會議? ('session_start();') – Rizier123 2015-01-04 14:21:28

+0

是的,我在配置文件中添加了session_start(),並將配置文件包含在新聞文件中。 require_once( '的config.php'); – Butterfly 2015-01-04 14:23:29

+0

然後請在這裏向我們展示這兩個腳本 – Rizier123 2015-01-04 14:23:54

回答

3

你只需要頭()後使用的exit()。

$_SESSION['msg']="Hello"; 
header("Location: account.php"); 
exit(); 

然後在account.php使用

echo $_SESSION['msg']; 

確保session_start();是要顯示/創建會話消息每一頁上。

而不是使用這種方式,我更喜歡使用下面的函數。 (來區分,如果它是錯誤消息或成功在引導HTML標記)

function _redirect($url=NULL, $message=NULL, $message_type=NULL){ 
     $_SESSION['sess_flash_message']= array(); 
     if($message){ 
      switch($message_type){ 
      case 'success': $_SESSION['sess_flash_message'][] = '<div class="alert alert-success"><button data-dismiss="alert" class="close" type="button">×</button>'.$message.'</div>';break; 
      case 'error': $_SESSION['sess_flash_message'][] = '<div class="alert alert-error"><button data-dismiss="alert" class="close" type="button">×</button>'.$message.'</div>';break; 
      case 'notice': $_SESSION['sess_flash_message'][] = '<div class="alert alert-info"><button data-dismiss="alert" class="close" type="button">×</button>'.$message.'</div>';break; 
      case 'warning': $_SESSION['sess_flash_message'][] = '<div class="alert alert-block"><button data-dismiss="alert" class="close" type="button">×</button>'.$message.'</div>';break; 
      default: $_SESSION['sess_flash_message'][] = $message; 
      } 
     } 
    if($url) { 
     header("Location: ".$url); 
    } else { 
     header("Location: ".$_SERVER['HTTP_REFERER']); 
    } 
    exit(); 
    ob_flush();  
} 

,並呼籲_redirect('login.php','You need to login first!','warning');

0

最有可能你還沒有開始或繼續會話

<?php 
session_start(); 

// your code 
?> 

另一種選擇是通過URL.This來傳遞你的消息,你怎麼頭趕走:

$myMessage = "<YOUR MESSAGE HERE>"; 
header("Location: http://www.somesite.com/news.php?mymessage=" . $myMessage); 

然後在news.php:

$theMessage = $_GET['mymessage']; 
+0

我無法傳遞URL上的任何變量。我想有漂亮的URL – Butterfly 2015-01-04 14:41:04

+0

從安全角度來看,我不會使用'$ _GET'建議,因爲它會打開您的網站以進行參數操作。即它允許每個人都擺弄URL來讓你的站點顯示一條消息。 – Bjorn 2015-01-04 14:41:12

3

你必須改變:

$_session['op_status'] 

到:

$_SESSION['op_status'] 

因爲$_SESSION是超全球性的,必須大寫。

亦作參考見:http://php.net/manual/en/language.variables.superglobals.php

另外你在這裏做一個比較:

$_SESSION['op_status'] == 'Already Exist'; 

,我想你要分配它,所以它改成這樣:

$_SESSION['op_status'] = 'Already Exist'; 

另外我希望你在你的if聲明中有一個條件,否則它不起作用。

注意:確保session_start();位於所有使用會話的頁面內。


側面說明:

您可以在測試環境中添加錯誤報告:

<?php 
    ini_set("display_errors", 1); 
    error_reporting(E_ALL); 

// rest of your code below