[英]how do i pass a variable to another php file without include?
how would I pass the variable $u to another php file without using include or anything similar? 我如何将变量$ u传递给另一个php文件而不使用include或类似的东西? Am I right in thinking that the u is fetched ($_GET) from the url?
我是否认为从网址中获取了u($ _GET)是正确的吗?
if(isset($_GET["u"])){
$u = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
}
Yes, you right. 是的,你正确的。 For your purposes you need to use
$_SESSION
and session mechanism or cookies. 为了您的目的,您需要使用
$_SESSION
和会话机制或cookie。
You could pass it via GET , POST or sessions . 您可以通过GET , POST或session传递它。
GET: 得到:
file1.php file1.php
<?php
$u = 'something';
echo '<a href="http://website.com/file2.php?u=' . $u . '">Click me!</a>';
?>
file2.php file2.php
<?php
if(isset($_GET['u'])) {
echo $_GET['u'];
}
?>
Sessions: 会话:
file1.php file1.php
<?php
session_start();
if(!isset($_SESSION['u'])) {
$_SESSION['u'] = 'something';
}
?>
file2.php file2.php
<?php
session_start();
if(isset($_SESSION['u'])) {
echo $_SESSION['u'];
}
?>
Yes, the 'u' is fetched from the url. 是的,从网址中提取了“ u”。 eg http://domain.tld/index.php?u=bob will produce 'bob' as 'u'.
例如http://domain.tld/index.php?u = bob会将'bob'生成为'u'。 Add u=whatever to the url and the 'u' will be on that page or if you don't want to do that you will need to store it in a session and/or cookie.
在网址中添加u =任何内容,“ u”将显示在该页面上,或者如果您不想这样做,则需要将其存储在会话和/或cookie中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.