单击<a href=""> content in text file is deleted

时间:2016-05-27 07:34:19

标签: php file

I have a problem : I want to store variable in every pages of my project, but each time I click on a link in my page, the folder is cleaning. I also try with session variable but it's the same.

My code first page:

<?php

echo '
     <form action"test.php" method="post">
         clé ak : <input type="text" name="ak" /><br><br>
         clé as : <input type="text" name ="as" />
         <input type="submit" value="submit">
      </form>
      <a href="get-key3.php" target="_blank">take token</a>';
?>

And the page test.php :

<?php

file_put_contents("cles.txt",$_POST["ak"]."\n".$_POST["as"]);

require_once('OvhApi.php');
$action=@$_GET["action"];
$ovh = new OvhApi();
$resp = array();

echo '<a href="test.php?action=1">info</a><br>
      <a href="test.php?action=2">domain</a><br>';
switch($action)
{
      case 1:
              $resp = $ovh->get('/me');
              var_dump($resp);
              break;
      case 2:
             $resp = $ovh->get('/domain');
             var_dump($resp);
             break;
}
?>

Thank's for your help.

2 个答案:

答案 0 :(得分:0)

您可以通过非常简单的方式执行此操作。首先,我们将创建链接。

<a href="test.php?clear=yes">Clear file content</a>

请注意,我已将clear=yes参数传递给test.php。我们现在将使用它来确保点击链接。

if (issset($_GET['clear']) && $_GET['clear'] == 'yes') {
    $filename = 'filetoclear.txt';
    $contents = "";

    // Clear file
    file_put_contentz($filename, $contents);

    // Redirect to test.php without clearing the file a second time
    header("Location: test.php");
    exit;
}

但是,如果你不想清除文件而且我理解你的问题是错的(责备在我身上):

if (isset($_REQUEST['append'])) {
    $filename = "thatonefile.txt";
    $contents = file_get_contents($filename);
    $contents .= $_REQUEST['append']."\n";

    header("Location: test.php");
    exit;
}

<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="append">
    <input type="submit" value="Go">
</form>

使用$_REQUEST代替$_POST$_GET将确保您的代码无论您是使用get还是post来调用它。

答案 1 :(得分:0)

作为初学者,我不确定这对你是否有用,但我也一直在玩文字文件。

$myfile = fopen("cles.txt", "a+") or die("Unable to open file!");
$ak = $_POST['ak']."\r\n";
fwrite($myfile, $ak);
$as = $_POST['as']."\r\n";
fwrite($myfile, $as);
fclose($myfile);

&#34; +&#34;这里代表:开放阅读和写作;如果该文件不存在,请尝试创建它。写入总是附加。 fopen()还有其他选项;功能

我使用的来源是: http://php.net/manual/en/function.fopen.php

相关问题