将字符串从一个PHP脚本传递到另一个脚本,而不使用链接
我有一个在script1.php中计算的字符串。我需要将这个字符串传递到script2.php。因此,我不想将字符串嵌入URL,然后传递。还有别的办法吗 解决这个问题有不同的可能性。 您提到的第一个是获取。(例如,作为链接,甚至通过curl或AJAX隐藏。使用curl PHP进行调用。使用AJAX在服务器上进行调用,以便用户可以在源代码中看到字符串)将字符串从一个PHP脚本传递到另一个脚本,而不使用链接,php,Php,我有一个在script1.php中计算的字符串。我需要将这个字符串传递到script2.php。因此,我不想将字符串嵌入URL,然后传递。还有别的办法吗 解决这个问题有不同的可能性。 您提到的第一个是获取。(例如,作为链接,甚至通过curl或AJAX隐藏。使用curl PHP进行调用。使用AJAX在服务器上进行调用,以便用户可以在源代码中看到字符串) 发布 第二种方法是通过POST 使用script1.php创建一个HTML表单,并让它将响应发送到script2.php <form me
发布 第二种方法是通过POST 使用script1.php创建一个HTML表单,并让它将响应发送到script2.php
<form method="post" action="script2.php">
<input type="hidden" name="myString" value="myValue" />
<input type="submit" style="/*you can stile me like a link*/" value="Click me" />
</form>
<?php
$myString = null;
if(isset($_COOKIE['MyString'])) $myString = $_COOKIE['MyString'];
?>
<?php
if(!isset($_GET['myString'])){
?>
<div id="content"></div>
<script type="text/javascript">
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "script2.php?myString="+localStorage.getItem("myString"), false); //Be careful! You have to urlescape the value if necessary
xhReq.send(null);
var serverResponse = xhReq.responseText;
document.getElementById("content").innerHtml = serverResponse; //Be careful. Escape HTML Tags if necessary here
</script>
<?php
}
else{
$myString = $_GET['myString'];
}
?>
<?php
session_start();
$myString = null;
if(isset($_SESSION['myString'])) $myString = $_SESSION['myString'];
?>
现在,您可以通过以下方式在script2.php中使用此字符串
<?php
$myString = null;
if(isset($_POST['myString')) $myString = $_POST['myString'];
?>
文件 如果这两个脚本位于同一台服务器上,则可以使用一个文件。在本例中,每个请求都会看到创建的字符串script1.php
<?php
$myString = "myValue";
file_put_contents("myString.txt", $myString);
?>
现在,script2.php可以读取文件的内容并使用它
<?php
$myString = file_get_contents("myString.txt");
?>
数据库/其他应用程序或backgroundworker 另一种可能性(与文件非常相似)是将字符串存储在数据库中的方式。然后可以再次读取该值并在script2.php中使用它。 如果您可以全局访问数据库,甚至可以像GET或POST一样将字符串从一台服务器分发到另一台服务器 您甚至可以启动一个本地应用程序(带有exec函数),为您存储信息。然后,script2.php可以再次执行以获取新应用程序的值
Cookies 当然,您可以将字符串保存在Cookie中。如果浏览器允许,您可以使用script2.php阅读
<form method="post" action="script2.php">
<input type="hidden" name="myString" value="myValue" />
<input type="submit" style="/*you can stile me like a link*/" value="Click me" />
</form>
<?php
$myString = null;
if(isset($_COOKIE['MyString'])) $myString = $_COOKIE['MyString'];
?>
<?php
if(!isset($_GET['myString'])){
?>
<div id="content"></div>
<script type="text/javascript">
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "script2.php?myString="+localStorage.getItem("myString"), false); //Be careful! You have to urlescape the value if necessary
xhReq.send(null);
var serverResponse = xhReq.responseText;
document.getElementById("content").innerHtml = serverResponse; //Be careful. Escape HTML Tags if necessary here
</script>
<?php
}
else{
$myString = $_GET['myString'];
}
?>
<?php
session_start();
$myString = null;
if(isset($_SESSION['myString'])) $myString = $_SESSION['myString'];
?>
script1.php
<?php
$myString = "myValue";
setcookie('MyString', $myString);
?>
<?php $myString = "myValue"; /*Be careful. your string must not contain ' otherwise you have to escape it!*/ ?>
<script type="text/javascript">
localStorage.setItem('myString', '<?php echo $myString; ?>');
</script>
<?php
session_start();
$myString = 'myValue';
$_SESSION['myString'] = $myString;
?>
脚本2.php
<form method="post" action="script2.php">
<input type="hidden" name="myString" value="myValue" />
<input type="submit" style="/*you can stile me like a link*/" value="Click me" />
</form>
<?php
$myString = null;
if(isset($_COOKIE['MyString'])) $myString = $_COOKIE['MyString'];
?>
<?php
if(!isset($_GET['myString'])){
?>
<div id="content"></div>
<script type="text/javascript">
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "script2.php?myString="+localStorage.getItem("myString"), false); //Be careful! You have to urlescape the value if necessary
xhReq.send(null);
var serverResponse = xhReq.responseText;
document.getElementById("content").innerHtml = serverResponse; //Be careful. Escape HTML Tags if necessary here
</script>
<?php
}
else{
$myString = $_GET['myString'];
}
?>
<?php
session_start();
$myString = null;
if(isset($_SESSION['myString'])) $myString = $_SESSION['myString'];
?>
使用此解决方案,您的数据存储在客户端。如果用户愿意,他可以查看、更改和操作数据。另一方面,您可以在服务器上保存存储
本地存储 与Cookie类似,您可以使用JavaScript代码将数据存储在本地存储器中。本地存储仅在客户端。如果您想在script2.php上获取数据,必须通过AJAX调用它。现在您可以处理数据了 script1.php
<?php
$myString = "myValue";
setcookie('MyString', $myString);
?>
<?php $myString = "myValue"; /*Be careful. your string must not contain ' otherwise you have to escape it!*/ ?>
<script type="text/javascript">
localStorage.setItem('myString', '<?php echo $myString; ?>');
</script>
<?php
session_start();
$myString = 'myValue';
$_SESSION['myString'] = $myString;
?>
解决这个问题有不同的可能性。
您提到的第一个是获取。(例如,作为链接,甚至通过curl或AJAX隐藏。使用curl PHP进行调用。使用AJAX在服务器上进行调用,以便用户可以在源代码中看到字符串)
发布
第二种方法是通过POST
使用script1.php创建一个HTML表单,并让它将响应发送到script2.php
<form method="post" action="script2.php">
<input type="hidden" name="myString" value="myValue" />
<input type="submit" style="/*you can stile me like a link*/" value="Click me" />
</form>
<?php
$myString = null;
if(isset($_COOKIE['MyString'])) $myString = $_COOKIE['MyString'];
?>
<?php
if(!isset($_GET['myString'])){
?>
<div id="content"></div>
<script type="text/javascript">
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "script2.php?myString="+localStorage.getItem("myString"), false); //Be careful! You have to urlescape the value if necessary
xhReq.send(null);
var serverResponse = xhReq.responseText;
document.getElementById("content").innerHtml = serverResponse; //Be careful. Escape HTML Tags if necessary here
</script>
<?php
}
else{
$myString = $_GET['myString'];
}
?>
<?php
session_start();
$myString = null;
if(isset($_SESSION['myString'])) $myString = $_SESSION['myString'];
?>
现在,您可以通过以下方式在script2.php中使用此字符串
<?php
$myString = null;
if(isset($_POST['myString')) $myString = $_POST['myString'];
?>
文件
如果这两个脚本位于同一台服务器上,则可以使用一个文件。在本例中,每个请求都会看到创建的字符串script1.php
<?php
$myString = "myValue";
file_put_contents("myString.txt", $myString);
?>
现在,script2.php可以读取文件的内容并使用它
<?php
$myString = file_get_contents("myString.txt");
?>
数据库/其他应用程序或backgroundworker
另一种可能性(与文件非常相似)是将字符串存储在数据库中的方式。然后可以再次读取该值并在script2.php中使用它。
如果您可以全局访问数据库,甚至可以像GET或POST一样将字符串从一台服务器分发到另一台服务器
您甚至可以启动一个本地应用程序(带有exec函数),为您存储信息。然后,script2.php可以再次执行以获取新应用程序的值
Cookies
当然,您可以将字符串保存在Cookie中。如果浏览器允许,您可以使用script2.php阅读
<form method="post" action="script2.php">
<input type="hidden" name="myString" value="myValue" />
<input type="submit" style="/*you can stile me like a link*/" value="Click me" />
</form>
<?php
$myString = null;
if(isset($_COOKIE['MyString'])) $myString = $_COOKIE['MyString'];
?>
<?php
if(!isset($_GET['myString'])){
?>
<div id="content"></div>
<script type="text/javascript">
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "script2.php?myString="+localStorage.getItem("myString"), false); //Be careful! You have to urlescape the value if necessary
xhReq.send(null);
var serverResponse = xhReq.responseText;
document.getElementById("content").innerHtml = serverResponse; //Be careful. Escape HTML Tags if necessary here
</script>
<?php
}
else{
$myString = $_GET['myString'];
}
?>
<?php
session_start();
$myString = null;
if(isset($_SESSION['myString'])) $myString = $_SESSION['myString'];
?>
script1.php
<?php
$myString = "myValue";
setcookie('MyString', $myString);
?>
<?php $myString = "myValue"; /*Be careful. your string must not contain ' otherwise you have to escape it!*/ ?>
<script type="text/javascript">
localStorage.setItem('myString', '<?php echo $myString; ?>');
</script>
<?php
session_start();
$myString = 'myValue';
$_SESSION['myString'] = $myString;
?>
脚本2.php
<form method="post" action="script2.php">
<input type="hidden" name="myString" value="myValue" />
<input type="submit" style="/*you can stile me like a link*/" value="Click me" />
</form>
<?php
$myString = null;
if(isset($_COOKIE['MyString'])) $myString = $_COOKIE['MyString'];
?>
<?php
if(!isset($_GET['myString'])){
?>
<div id="content"></div>
<script type="text/javascript">
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "script2.php?myString="+localStorage.getItem("myString"), false); //Be careful! You have to urlescape the value if necessary
xhReq.send(null);
var serverResponse = xhReq.responseText;
document.getElementById("content").innerHtml = serverResponse; //Be careful. Escape HTML Tags if necessary here
</script>
<?php
}
else{
$myString = $_GET['myString'];
}
?>
<?php
session_start();
$myString = null;
if(isset($_SESSION['myString'])) $myString = $_SESSION['myString'];
?>
使用此解决方案,您的数据存储在客户端。如果用户愿意,他可以查看、更改和操作数据。另一方面,您可以在服务器上保存存储
本地存储
与Cookie类似,您可以使用JavaScript代码将数据存储在本地存储器中。本地存储仅在客户端。如果您想在script2.php上获取数据,必须通过AJAX调用它。现在您可以处理数据了
script1.php
<?php
$myString = "myValue";
setcookie('MyString', $myString);
?>
<?php $myString = "myValue"; /*Be careful. your string must not contain ' otherwise you have to escape it!*/ ?>
<script type="text/javascript">
localStorage.setItem('myString', '<?php echo $myString; ?>');
</script>
<?php
session_start();
$myString = 'myValue';
$_SESSION['myString'] = $myString;
?>
要在站点内从A到B获取数据,您可以使用:获取、发布、会话、cookie、本地存储、缓存或数据库。每一种都有其优点和缺点。如果会话/获取/发布在循环中频繁设置,会话/获取/发布会起作用吗?要在站点内从a到B获取数据,可以使用:获取、发布、会话、cookie、本地存储、缓存或数据库。每一种都有其优点和缺点。仔细阅读其中的每一个,以后会非常有用。如果SESSION/GET/POST在循环中频繁设置,SESSION/GET/POST会起作用吗?