AdrienG AdrienG - 6 months ago 24
PHP Question

PHP after a click on <a href> content in text file is deleted

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.

Answer

As a beginner I'm not sure if this will be usefull to you, but I have been playing with text-files as well.

$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);

"a+" here stands for: Open for reading and writing; If the file does not exist, attempt to create it. Writes are always appended. There are other options with fopen(); function.

The source I used was: http://php.net/manual/en/function.fopen.php