2015-01-04 52 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