0

I am trying to rename a html when i click on submit button. the problem is that it will rename the page while the page loads even thu i did not click on the submit button, is there any to avoid that. i wan it to load only on submit?

   <form method="post" action="<?php rename("log.html","OLD.LOG/log.html.bac.date");  $file=fopen("log.html","w+"); ?>">
            <input name="Rename" type="submit"  id="Rename" value="Clear Page" >
            <p class="logout"><a id="exit" href="#">Exit</a></p>
            </form></p>
3
  • Uhh, what do you think your code does? Commented Jan 21, 2014 at 15:30
  • It renames the log.thml to log.html.bac then creates a new html file called log.html Commented Jan 21, 2014 at 15:40
  • PHP does it's thing before HTML takes over, so saying that it will change AS IT'S LOADING is false. Commented Jan 21, 2014 at 15:44

2 Answers 2

1

If I understood your question correctly, you have a form and clicking of the "Rename" button should rename "log.html" to "OLD.LOG/log.html.bac.date" in the PHP server. I strongly discourage letting client side decide the name of the renamed file. With your current approach, the client side (browser) sends details of the renaming operation. This could be a security loop hole using which an attacker could rename an important file in the server.

Assuming that you are doing a simple form submit (no fancy ajax required), change the action to point to a php file in the server.

   <form method="post" action="/rename">
        <input name="Rename" type="submit"  id="Rename" value="Clear Page" >
        <p class="logout"><a id="exit" href="#">Exit</a></p>
   </form></p>

I assumed that you have setup clean URLs with some bootstrap/front controller to execute rename.php in the PHP server otherwise, use

<form method="post" action="/rename.php">

then in rename.php have code similar to the following

<?php 
rename("log.html","OLD.LOG/log.html.bac" . time());  
$fh = fopen("log.html","w"); 
fclose($fh);
?>

I added the time() to prevent the back up file being overridden every single time. And always close the file handlers you open. I also assumed that you only want to create the empty log.html, and not to read it. so w is enough, w+ is not required. Also, I assume that you have some authentication/authorization measures in place to prevent a DOS attack on the server by executing the rename operation too many.

If you want to use the same PHP file that outputs the form then pass an extra parameter in the POST request to the same URL. There are many ways you can do that, the simplest being that. But you can do it properly using proper HTTP headers, but having the same URL for displaying the form as well as for processing the form. There are many MVC frameworks like Zend framework which are capable of routing to different actions in the same controller by inspecting the HTTP method (GET, POST, DELETE, ect)

Sign up to request clarification or add additional context in comments.

1 Comment

Very well assumed. I have a log of Chat files, i added this feather for the admins of the chat. to clear the chat log. I took your code that you added i modified it a bit. now this is awesome. Thanks man
1

The PHP engine will execute the code between <?php and ?> as it generates the HTML. It can't do anything conditional on what it is outputting.

You need to move that code into a separate .php file and place the URL of that file in the action attribute.

2 Comments

I would of dont that already lol I need it to run on the same page.
Then set the action to the URL of the same page, move the file renaming code to somewhere sensible (i.e. not in the middle of the code that outputs the HTML for a form) and add an conditional to test if the form has been submitted or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.