简体   繁体   English

我如何在不包含的情况下将变量传递给另一个PHP文件?

[英]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 . 您可以通过GETPOSTsession传递它。

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.

相关问题 如何包含另一个存储为变量的PHP文件? - How do I include another PHP file that is stored as a variable? 如何使用include函数通过URL将变量/值传递给另一个PHP文件(通过HTTP)? (PHP) - how do I pass a variable/value via URL to another PHP file (via HTTP) using the include function? (PHP) 如何将php变量传递给.php include? - How do I pass a php variable to a .php include? 如何在不包含的情况下将变量传递到另一个页面 - How to pass variable to another page without include 如何在不使用include或require的情况下在php中包含文件? - How do I to include a file in php without using include or require? 如何在不将该语法弄乱变量的情况下,将一个PHP URL作为php变量传递给另一个URL? - How do I pass one PHP url as a php variable in another URL without that syntax messing up the variables? 如何将变量传递给包含的文件? - How do I pass a variable to a file that I include? 如何通过将该文件包含在另一个php文件中来将POST变量值提取到另一个php文件中? - How can I fetch POST Variable value into another php file by include that file in another php file? 如何将PHP变量从一个文件多次传递到另一个文件? - How do I pass a PHP variable from one file to another multiple times? 如何包含另一个 PHP 文件中的数组 - How do I include array from another PHP file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM