2016-05-27 14 views
0

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.クリックした後のPHP <a href> content in text file is deleted

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'); 
[email protected]$_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.

+0

フォームにメソッドポストがある場合、どのようにGET変数を取得しようとしていますか? –

+0

セッションで変数を渡すには、各ページにsession_start()が必要です。 –

+0

これはあなたの主な問題ではありませんが、悪い不要な 'fclose(" cles.txt ");'を取り出してください。 –

答えて

0

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

+0

ありがとうございます! – AdrienG

0

You can do this in a very simple manner. First we will create the link.

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

Note that I have passed a clear=yes parameter to test.php. We will now use this to make sure the link has been clicked.

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, however you didn't want to clear the file and I understood your question wrong (Blame's on me) :

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> 

Using $_REQUEST instead of $_POST or $_GET will make sure that your code works whether you are calling it using get or post.

+0

私はクリアしたくありません、 'test.php'のリンクをクリックすると自動的にクリアされます。もし私が明確でないなら、私は残念です。ああ、すみません、私はリフレッシュしませんでした。 – AdrienG

+0

@AdrienG私の回答を更新しました – Peter

+0

このコードブロックはすべて同じページにありますか? – AdrienG

関連する問題