php 在会话中存储错误消息并显示在另一个页面上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28251555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
store error message in session and display on another page
提问by kavi
there is a scenario that i have in my hand at present.I have 2 pages index.php and code.php.
我现在手头有一个场景。我有 2 页 index.php and code.php.
the user first goes to index.php page and then is redirected to code.php page and back to index.php page.
用户首先转到 index.php 页面,然后重定向到 code.php 页面并返回 index.php 页面。
in case some error occurs in code.php page, i need to display that error on index.php page when it gets redirected back to index page.
如果在 code.php 页面中发生一些错误,当它被重定向回索引页面时,我需要在 index.php 页面上显示该错误。
i stored the error message in session like this and then redirected the page bsck to index.php page
我像这样在会话中存储错误消息,然后将页面 bsck 重定向到 index.php 页面
$_SESSION["errormsg"]='please try again';
header("Location: index.php");
but if i echo this session message in index.php page it gets displayed evertime when the page gets loaded, however i want only once when the index page comes after the code.php page. can anyone tell how it can be done
但是,如果我在 index.php 页面中回显此会话消息,它会在页面加载时每次显示,但是当索引页面出现在 code.php 页面之后时,我只想要一次。谁能告诉我怎么做
回答by Pulkit Pithva
I think that, the best way to show error message via session is having separate message file like message.php.
我认为,通过会话显示错误消息的最佳方式是使用单独的消息文件,如 message.php。
Step 1 : you just have to set the session with your error message in your code.php.
第 1 步:您只需在 code.php 中使用错误消息设置会话。
$_SESSION["errormsg"]='please try again';
$_SESSION["errormsg"]='请再试一次';
Step 2 :
第2步 :
Now create new file with the name "message.php".
Store the session error message into new variable.
现在创建名为“message.php”的新文件。
将会话错误消息存储到新变量中。
$error = $_SESSION["errormsg"];
$error = $_SESSION["errormsg"];
Step 3 :
第 3 步:
then, unset the session or destroy the session.
然后,取消设置会话或销毁会话。
// remove all session variables
session_unset();
// destroy the session
session_destroy();
Step 4 :
第四步 :
- Now echo that error variable in formatted div tag so that your error looks good and also eye catchy.
- 现在在格式化的 div 标签中回显该错误变量,以便您的错误看起来不错并且引人注目。
<div id='alert'><div class=' alert alert-block alert-info fade in center'>$error</div></div>
- Now, in you index.php file, have a method to check that if the session variable "errormsg" is set or not.
- If the session variable is set then call the message.php file right away. it will echo that div in you index.php file .
- 现在,在您的 index.php 文件中,有一个方法来检查会话变量“errormsg”是否已设置。
- 如果设置了会话变量,则立即调用 message.php 文件。它会在你的 index.php 文件中回显那个 div。
I think this would give you full answer.
我想这会给你完整的答案。
回答by Vikas Arora
Use a simple condition to check if the particular session is set or not.
使用简单的条件来检查是否设置了特定会话。
if(isset($_SESSION["errormsg"])) {
$error = $_SESSION["errormsg"];
session_unset($_SESSION["errormsg"]);
} else {
$error = "";
}
echo $error;
回答by Aadil Keshwani
You can use session_start()
at starting of page as you are assigning session variable value without actually starting the session.
您可以session_start()
在页面开始时使用,因为您正在分配会话变量值,而无需实际启动会话。
Also you can use the condition
你也可以使用条件
<?php
session_start();
if (isset($_SESSION['variablename']))
{
//your code
}
?>
回答by Abdo-Host
//Ensure that a session exists (just in case)
if( !session_id() )
{
session_start();
}
Function
功能
/**
* Function to create and display error and success messages
* @access public
* @param string session name
* @param string message
* @param string display class
* @return string message
*/
function flash( $name = '', $message = '', $class = 'success fadeout-message' )
{
//We can only do something if the name isn't empty
if( !empty( $name ) )
{
//No message, create it
if( !empty( $message ) && empty( $_SESSION[$name] ) )
{
if( !empty( $_SESSION[$name] ) )
{
unset( $_SESSION[$name] );
}
if( !empty( $_SESSION[$name.'_class'] ) )
{
unset( $_SESSION[$name.'_class'] );
}
$_SESSION[$name] = $message;
$_SESSION[$name.'_class'] = $class;
}
//Message exists, display it
elseif( !empty( $_SESSION[$name] ) && empty( $message ) )
{
$class = !empty( $_SESSION[$name.'_class'] ) ? $_SESSION[$name.'_class'] : 'success';
echo '<div class="'.$class.'" id="msg-flash">'.$_SESSION[$name].'</div>';
unset($_SESSION[$name]);
unset($_SESSION[$name.'_class']);
}
}
}
//Set the first flash message with default class
flash( 'example_message', 'This content will show up on example2.php' );
//Set the second flash with an error class
flash( 'example_class', 'This content will show up on example2.php with the error class', 'error' );
Displaying the messages
显示消息
<?php flash( 'example_message' ); ?>
<?php flash( 'example_class' ); ?>
回答by d.dean
The question has been answered 3 years ago, but i thought i could add something into the same topic. I hope it is helpful.
该问题已在 3 年前得到回答,但我认为我可以在同一主题中添加一些内容。我希望它有帮助。
If session message in index.php page gets displayed every time you open the page, it means that session variable "errormsg" has not been cleared.
如果每次打开页面都显示 index.php 页面中的 session 消息,则表示 session 变量“errormsg”尚未清除。
Therefore use unset($_SESSSION['errormsg']);
to remove it after display as suggested above.
因此unset($_SESSSION['errormsg']);
,按照上面的建议在显示后将其删除。
However when you do this, another problem occurs the error message wont be displayed with this code in code.php:
但是,当您这样做时,会出现另一个问题,该错误消息不会与 code.php 中的此代码一起显示:
$_SESSION["errormsg"]='please try again';
header("Location: index.php");
Because when you redirect to the index page, session variable is cleared before message gets displayed.
因为当您重定向到索引页面时,会话变量会在消息显示之前被清除。
To solve this:
要解决这个问题:
Avoid redirecting the page more than once.
避免多次重定向页面。
in code.php use:
在 code.php 中使用:
$_SESSION["errormsg"]='please try again';
without redirecting to the index.php file.
无需重定向到 index.php 文件。