在不同的 php 文件之间使用 SESSIONS 变量 [英] Use SESSIONS variables between different php files
问题描述
我是 $_SESSIONS
的新手,但需要它在不同的 php 文件之间重用变量.在下面的代码中,我想在另一个 php 文件中使用变量 $word
,但我不确定如何执行此操作.
I am new to $_SESSIONS
but needs it to re-use variables between different php files. In the below code I want to use the variable $word
in another php file, but I am unsure how to do this.
我的php 文件如下所示:
<?php
if (isset($_POST["search"])) {
//include database connection
$word = mysql_real_escape_string($_POST["search"]);
$word = htmlentities($word);
$sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10");
$results = mysql_query($sql);
if (mysql_num_rows($results)==0) {
echo $word, " text bla";
}else {
echo $word, " text bla bla";
while ($row = mysql_fetch_assoc($results)) {
echo '<pre>', print_r($row), '<pre>';
}
}
}?>
期待您的建议.
---更新会话仍然无法在 page2.php 上工作?---
我不明白为什么 $_SESSION
不起作用.一个 page1.php 我可以 echo($_SESSION['word'])
并获得正确的值,但是一个 page2.php 我得到 ('$'."_SESSION['word'] 未设置,因为您从未访问过文件 1");
I do not understand why $_SESSION
do not work. One page1.php I can echo($_SESSION['word'])
and get the correct value, but one page2.php I get ('$'."_SESSION['word'] isn't set because you had never been at file one");
我测试了以下所有解决方案,但没有一个有效 = page2.php 上的结果相同.
I tested all the below solutions but none of them worked = same result on page2.php.
我的 page1.php 文件.
<?php
session_start();
//if we got something through $_POST
if (isset($_POST["search"])) {
// include database connection
$connect = mysql_connect('localhost', 'root', 'NomiS123') or die(mysql_error());
mysql_select_db('workcard');
// sanitize user input
$word = mysql_real_escape_string($_POST["search"]);
$word = htmlentities($word);
// build search query to the database
$sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10");
// get results
$_SESSION['word'] = $word;
$results = mysql_query($sql);
if (mysql_num_rows($results)==0) {
$_SESSION['word'] = $word;
echo($_SESSION['word']. "<br>");
var_dump($_SESSION);
echo "<br>";
echo "link link <br>";
echo "<a href=\"../page2.php/\">new card</a> <br>";
echo "<a href=\"//cykelc/\">New Search</a>";
} else {
echo $word, " bla bla text <br> Create card <br>";
echo "Edit info on: ", $word, "<br>";
echo "<a href=\"//cykelc/\">New Search</a> <br>";
while ($row = mysql_fetch_assoc($results)) {
echo '<pre>', print_r($row), '<pre>';
}
//$results->free();
}
}
// mysql_close($connect);
?>
我的 PAGE2.php 文件.
<?php
session_start();
if(isset($_SESSION['word'])) {
$word = $_SESSION['word'];
echo($word);
} else {
die('$'."_SESSION['word'] isn't set because you had never been at file one");
}
?>
我快要疯了.
更新 - 已解决
我测试了以下所有建议,但它们都不起作用,这很奇怪,因为我可以在 page1.php 和 page2.php 上设置和回显 sesson_id()
,但在 page2.php 上我得到了不同的sesson_id()
.我开始查看我的 MAMP 会话设置,但一切都设置正确.解决方案是简单地"将 session_start();
放在 page2.php 的最顶部.最重要的是我的意思是在一切甚至<!DOCTYPE html>
等之前
解决+经验教训:-)
UPDATE - SOLVED
I tested all the below suggestions but none of them worked which was weird because I could set and echo out the sesson_id()
on page1.php and page2.php, but on page2.php I got a different sesson_id()
. I began to look into my MAMP sessions settings, but everything was correct set. The solution was "simply" to place the session_start();
on the very top on page2.php. And by the very top I mean before everything even the <!DOCTYPE html>
etc.
Solved + lesson learned :-)
推荐答案
首先你必须在打开 PHP 'tag' ()
First you must start the seesion via session_start();
directly after the opening PHP 'tag' (<?php session_start();... ?>
)
然后您必须将变量保存到会话中.为此,您可以使用 $_SESSION['word'] = $word;
.
Then you must save your variable to the session.
You can use $_SESSION['word'] = $word;
for this purpose.
并且在另一个文件中,您还必须在 <?php
'标签' 之后的第一个位置使用 session_start();
.
And in the other file you must also use session_start();
at the very first after the <?php
'tag'.
然后您可以通过 $word = $_SESSION['word'];
访问旧变量.
Then you could access the old variable via $word = $_SESSION['word'];
.
您现在还可以在第二个文件中使用 $word
.但是你只能在它设置的情况下使用它(并且你之前在第一个文件中的位置).
You now can also use $word
in the second file. But you only can use it if it's set (and you where at the first file before).
文件一:
<?php
session_start();
if (isset($_POST["search"])) {
//include database connection
$word = mysql_real_escape_string($_POST["search"]);
$word = htmlentities($word);
$_SESSION['word'] = $word;
$sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10");
$results = mysql_query($sql);
if (mysql_num_rows($results)==0) {
echo $word, " text bla";
}else {
echo $word, " text bla bla";
while ($row = mysql_fetch_assoc($results)) {
echo '<pre>', print_r($row), '<pre>';
}
}
}?>
文件二:
<?php
session_start();
if(isset($_SESSION['word'])) {
$word = $_SESSION['word'];
} else {
die('$'."_SESSION['word'] isn't set because you had never been at file one");
}
echo $word;
?>
希望这有帮助;)
这篇关于在不同的 php 文件之间使用 SESSIONS 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!