简体   繁体   English

多页中的PHP Session变量

[英]PHP Session variables in multiple pages

So, here I have page1.php: 因此,这里有page1.php:

<form action="action_form.php" method="post">
<select name="font_syle">
<option value="tahoma">Tahoma</option>
<option value="arial">Arial</option>
</select>
<input type="submit" value="Done" />
</form>

Here action_form.php: 这里是action_form.php:

<?php
session_start();
$font_style = $_POST["font_syle"];
$_SESSION["font_syle"] = $font_style;
if($_SESSION["font_syle"] == 'tahoma') $font_style = 10;
else if($_SESSION["font_syle"] == 'arial') $font_style = 20;

$total = $font_style;

echo $total;
?>

And here page.php 而这里page.php

<?php 
ob_start();
include 'action_form.php';
ob_end_clean();

echo $total;
?>

I don't know why the value of "$total" is not printed on page.php 我不知道为什么“ $ total”的值未打印在page.php上

page.php includes action_form.php . page.php包含action_form.php That sets the value of $font_style to: $font_style的值设置为:

$font_style = $_POST["font_syle"];

Since page.php hasn't just been posted through a form, it's setting $font_style to an empty string. 由于page.php不仅通过表单发布,因此将$font_style设置$font_style空字符串。 So when you come to echo it out, there's nothing there to echo. 因此,当您要回声它时,就没有回声了。

You can do echo $_SESSION["font_syle"]; 您可以做echo $_SESSION["font_syle"]; in the page.php to print it 在page.php中进行打印

The reason is your form is going to action_form.php and store the data inside the variable $_SESSION . 原因是您的表单将转到action_form.php并将数据存储在变量$_SESSION

When you open page.php the data doesn't exist anymore because $total does not move between page. 当您打开page.php ,数据不再存在,因为$total不会在页面之间移动。

The solution here is to change : 解决方案是更改:

<form action="action_form.php" method="post">

for 对于

<form action="page.php" method="post">

OR 要么

Print out the session variable instead. 而是打印出会话变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM