2016-05-27 69 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

相關問題