将变量从一个php文件传递到另一个

时间:2015-10-11 19:00:46

标签: php html

我目前在file1.php中,我想要一个来自file1.php的变量传递给file2.php

如何将变量从一个php文件传递到另一个?

我也想从file1.php转到file2.php

我在file1.php中执行了以下操作:

<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<h1>Animation</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi, unde amet a repellat tempora ipsum! Maiores minima sapiente blanditiis, asperiores necessitatibus corporis incidunt! Ex, praesentium aliquam omnis dicta quo. Quisquam.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Libero eius eligendi praesentium doloremque voluptatibus, vero sed aspernatur officia eum repellat unde dolore ut eaque esse, officiis natus harum vel eos.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta atque beatae temporibus et officiis enim, rem facere! Voluptatibus adipisci, odit in necessitatibus explicabo eveniet culpa. Nisi praesentium quasi error ducimus!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minus autem facere ipsa ratione excepturi praesentium. Reiciendis aut praesentium earum laboriosam, architecto quasi error, corporis, nesciunt maxime distinctio quo nisi debitis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati eos, consequatur voluptatibus harum deserunt! Fugit voluptatem veniam atque, odio aut qui iste laudantium, nemo error vero natus eaque unde sunt!</p>
<p><img src="https://assets.keycdn.com/img/cdn-network.svg" alt="" class="animated" /></p>

并在file2.php中执行了以下操作:

session_start();
$_SESSION['akknum']="jkl";
header("Location: file2.php");

file2.php正在打开,abc正在打印,但没有打印“jkl”的值。我做错了什么?

3 个答案:

答案 0 :(得分:2)

您需要在session_start();中添加file2.php

session_start();
echo "abc";
echo $_SESSION['akknum'];

Basic usage

答案 1 :(得分:2)

你也需要session_start第二个文件。

答案 2 :(得分:2)

如果您想在其他文件中使用$_SESSION,则需要致电session_start();

<强> file1.php

<?php
session_start();
$_SESSION['akknum']="jkl";
header("Location: file2.php");
?>

<强> file2.php

<?php
session_start();
echo $_SESSION['akknum'];
//jkl
?>

详细了解php sessions