如何在代码完成 运行 文件时不重置包含文件中的静态文件?
how to make static file in a included file that doesn't reset when the code finishes running the file?
我在主 index.php 文件的顶部包含了一个文件,但我无法在其中创建一个变量(包含的文件)常量(静态)
--更多信息--: 所以我正在尝试制作一个表格。每次用户成功提交时,表单都应该创建一个新文件(包含他的名字和一段他的反馈)。所以,我制作了一个计数器并将其附加到文件名,所以每次提交时..都会创建一个新文件
static $counter = 3;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// validation code
if( /* checking that the submission is successful */){
$file = "feedback". $GLOBALS['counter'] .".txt";
file_put_contents( $file, $feedback_paragraph );
$GLOBALS[ 'counter' ] = $GLOBALS [ 'counter' ] + 1;
}
}
问题是计数器保持不变。它只是增加到 4 并在再次提交时重置回 3。不知道为什么?
注意:我知道这不是实现此类项目的最佳方式,但我仍然想了解 php 是如何工作的以及为什么这段代码不起作用?
谢谢大家。你们真是一个了不起的社区!
我认为那是因为您使用的 "static" 会将您的计数器重置为 3。
试试这个,
1.提交
2. 将 $counter 记录到 file.txt
3. 递增 $counter
4. 再次提交,但这次使用递增的计数器 file.txt
5. 重复步骤2~4.
PHP 是无状态的。每次发出请求时,脚本都是 re-run,在您的情况下,它会将计数器设置为 3.
如果你想在请求之间持久化计数器,你需要使用一些其他机制来持久化它,比如单独的文件、数据库等。
你不应该为了你看起来正在做的事情而将那个变量设为静态。只在页面上出现一次就足够了,或者在需要的地方将其声明为全局变量(对深入了解全局很有帮助,看起来你有点熟悉?)
只是一个问题。我不知道 $counter 在任何地方是否等于 $GLOBALS['counter'] 。这发生在某处吗?
例如:$counter = $GLOBALS['counter'];或者 $GLOBALS['counter'] = $counter;
@Moaz Eldefrawy 我为你写了非常简单的脚本,我知道它不是最佳的但出于教育目的我写了尽可能多的细节,将此代码粘贴到你的本地主机并尝试一下。
此代码会将名称和反馈的数据记录在文本文件中作为 "feedback(counter value).txt",在您的目录中创建 counter.txt 以供计数。
检查您的目录并尝试打开 "feedback(counter value).txt" 以获取记录的数据。
希望这对您有所帮助:)
<?php
//initialize
$name = "";
$feedback = "";
$counter_file = "counter.txt";
//Check if name field has input
if(isset($_POST['name']) && ($_POST['name']) != "") {
$name = $_POST['name'];
}
//Check if feedback has input
if(isset($_POST['feedback']) && ($_POST['feedback']) != "") {
$feedback = $_POST['feedback'];
}
//if name field and feedback has input call check_counter and record_counter functions
if((!empty($name)) && (!empty($feedback))) {
check_counter($counter_file);
record_data($name, $feedback, $counter_file);
}
//assuming no counter.txt is created in your directory attempt to create counter.txt
function check_counter($counter_file) {
if(file_exists($counter_file) == false) {
$file = fopen($counter_file, 'a'); //if no counter.txt file in your directory create one
$counter = 0; //initialize counter from 0
} else {
$file = fopen($counter_file, 'a+'); //if counter.txt file exist open that file
$counter = file_get_contents($counter_file); //read the counter as string
$counter++; //increment counter
}
file_put_contents($counter_file, $counter); //value of counter is recorded in counter.txt
fclose($file); //close the file
}
function record_data($name, $feedback, $counter_file) {
$file = fopen($counter_file, 'a+'); //open counter.txt file
$counter = file_get_contents($counter_file); //read the counter value of file
$txt = fopen('feedback'.$counter.'.txt', 'a'); //open file, if no file exist attempt to create it
$data = "name: ".$name.PHP_EOL."feedback: ".$feedback;
fwrite($txt, $data); //record the name,feed back value as 'feedback$counter.txt'
fclose($file); //close the file
}
?>
<html>
<head>
<title>record_data</title>
</head>
<body>
<form action="index.php" method="post">
<table>
<tr>
<td>User Name: </td><td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Feedback: </td><td><textarea name="feedback" /></textarea></td>
</tr>
<tr>
<td></td><td><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>
我在主 index.php 文件的顶部包含了一个文件,但我无法在其中创建一个变量(包含的文件)常量(静态)
--更多信息--: 所以我正在尝试制作一个表格。每次用户成功提交时,表单都应该创建一个新文件(包含他的名字和一段他的反馈)。所以,我制作了一个计数器并将其附加到文件名,所以每次提交时..都会创建一个新文件
static $counter = 3;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// validation code
if( /* checking that the submission is successful */){
$file = "feedback". $GLOBALS['counter'] .".txt";
file_put_contents( $file, $feedback_paragraph );
$GLOBALS[ 'counter' ] = $GLOBALS [ 'counter' ] + 1;
}
}
问题是计数器保持不变。它只是增加到 4 并在再次提交时重置回 3。不知道为什么?
注意:我知道这不是实现此类项目的最佳方式,但我仍然想了解 php 是如何工作的以及为什么这段代码不起作用?
谢谢大家。你们真是一个了不起的社区!
我认为那是因为您使用的 "static" 会将您的计数器重置为 3。
试试这个,
1.提交
2. 将 $counter 记录到 file.txt
3. 递增 $counter
4. 再次提交,但这次使用递增的计数器 file.txt
5. 重复步骤2~4.
PHP 是无状态的。每次发出请求时,脚本都是 re-run,在您的情况下,它会将计数器设置为 3.
如果你想在请求之间持久化计数器,你需要使用一些其他机制来持久化它,比如单独的文件、数据库等。
你不应该为了你看起来正在做的事情而将那个变量设为静态。只在页面上出现一次就足够了,或者在需要的地方将其声明为全局变量(对深入了解全局很有帮助,看起来你有点熟悉?)
只是一个问题。我不知道 $counter 在任何地方是否等于 $GLOBALS['counter'] 。这发生在某处吗?
例如:$counter = $GLOBALS['counter'];或者 $GLOBALS['counter'] = $counter;
@Moaz Eldefrawy 我为你写了非常简单的脚本,我知道它不是最佳的但出于教育目的我写了尽可能多的细节,将此代码粘贴到你的本地主机并尝试一下。
此代码会将名称和反馈的数据记录在文本文件中作为 "feedback(counter value).txt",在您的目录中创建 counter.txt 以供计数。
检查您的目录并尝试打开 "feedback(counter value).txt" 以获取记录的数据。
希望这对您有所帮助:)
<?php
//initialize
$name = "";
$feedback = "";
$counter_file = "counter.txt";
//Check if name field has input
if(isset($_POST['name']) && ($_POST['name']) != "") {
$name = $_POST['name'];
}
//Check if feedback has input
if(isset($_POST['feedback']) && ($_POST['feedback']) != "") {
$feedback = $_POST['feedback'];
}
//if name field and feedback has input call check_counter and record_counter functions
if((!empty($name)) && (!empty($feedback))) {
check_counter($counter_file);
record_data($name, $feedback, $counter_file);
}
//assuming no counter.txt is created in your directory attempt to create counter.txt
function check_counter($counter_file) {
if(file_exists($counter_file) == false) {
$file = fopen($counter_file, 'a'); //if no counter.txt file in your directory create one
$counter = 0; //initialize counter from 0
} else {
$file = fopen($counter_file, 'a+'); //if counter.txt file exist open that file
$counter = file_get_contents($counter_file); //read the counter as string
$counter++; //increment counter
}
file_put_contents($counter_file, $counter); //value of counter is recorded in counter.txt
fclose($file); //close the file
}
function record_data($name, $feedback, $counter_file) {
$file = fopen($counter_file, 'a+'); //open counter.txt file
$counter = file_get_contents($counter_file); //read the counter value of file
$txt = fopen('feedback'.$counter.'.txt', 'a'); //open file, if no file exist attempt to create it
$data = "name: ".$name.PHP_EOL."feedback: ".$feedback;
fwrite($txt, $data); //record the name,feed back value as 'feedback$counter.txt'
fclose($file); //close the file
}
?>
<html>
<head>
<title>record_data</title>
</head>
<body>
<form action="index.php" method="post">
<table>
<tr>
<td>User Name: </td><td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Feedback: </td><td><textarea name="feedback" /></textarea></td>
</tr>
<tr>
<td></td><td><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>