Jump to content

Button to change PHP file contents


adamjones

Recommended Posts

Ok.

The other day, I submitted a question on how to make a 'Maintenance Script'. (http://www.phpfreaks.com/forums/index.php/topic,217623) I was wondering if it's possible to make a button, that when pressed will change the php file from;

 

<?PHP

define('MAINTENANCE', true);

?>

 

To

 

<?PHP

define('MAINTENANCE', false);

?>

 

And then back again, if pressed.

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/125320-button-to-change-php-file-contents/
Share on other sites

button posts right?

<?php session_start(); ?>
<?php $_SESSION['Maint'] = $_POST['hiddenfield'] ?>
<?php if(isset($_SESSION['Maint']) && $_SESSION['Maint'] == "true") { ?> //your button code and hiddenfield with value "false"
<?php } else { ?> //the same button and hidden field but with value "true" <?php } ?>

it creates a session variable based on what your button posts. Really, your button isn't posting any value, you need a hidden field with the value of either true or false. Initially I create a session

<?php session_start(); ?>

Then I tell it to create a session variable out of the value they posted using the button.

<?php $_SESSION['Maint'] = $_POST['hiddenfield'] ?>

Then I check to see if they have already clicked the button once so I use "isset()" and then I see if it is true because that is what the value would be if the button has only been clicked once (right?)

<?php if(isset($_SESSION['Maint']) && $_SESSION['Maint'] == "true") { ?> 

after you do that, you just simply need a form with a hidden field with the value "false" and a button to submit. have the form action as something like "?", there are a number of ways for the form to post to self... and of course method="POST".

 

<?php } else { ?>

this makes it so that the above form only shows if they have already clicked it once. Below that is what you'll need for the button for them to click the first time and then again if they turn it to false again. you know, people don't make up there minds. lol

so just copy your first form stuff and change the hidden field value to "true"... close this all up with

<?php } ?>

 

Ask Mchl said, if you want it for every one who is that the site at that time to suddenly have maintnence on, then you'll need a file or a db. You could also do this with the Get function and completely leave out the session thing. I just like it cuz it doesn't show the user how you coded it.

if you want every single person viewing the site to all go to the maintenance version of the site, you'll need to use a database or a file, like Mchl said. I don't know much about the file writing thing, but I can tell you that if you have a database already, using it would be you best option. Or, Mchl or someone can inform us of some file writing tactics. I don't know why though you'd want every one on the same version of the page like that. Odd.

Right..  ???

 

Ok. What I'm trying to do, for an admin page, is have a button, saying, for example, 'Turn Site On/Off'. Currently on all my pages, I have this script;

 

<?PHP include("check.php"); ?>

<?PHP if(MAINTENANCE == true) {

header('location:./error/maintenance.php');

die();

}

?>

 

And I also have another PHP file named 'check.php', which contains this;

 

<?PHP

 

define('MAINTENANCE', true);

 

?>

 

When I set '('MAINTENANCE', ___);' to 'true', it redirects people on the pages to '/error/maintenance.php'. Or if set to false, it does nothing. At the moment, I have to change it from 'true' to 'false', or vice-versa manually. I was just wondering If I could make a button for my admin page to do it for me?

 

Sorry if I'm being buggy. I'm new to PHP.

 

Cheers.

I use a maintenace mode in my script, and because I have my application written so you can have multiple installs, I made it so that it's stored in a table in a DB. The table has `id` int(2), `install_name` varchar(20), and `status` int(1)

 

This way, all I need to do is add this to my config file:

<?php
$site_name = "My Site Name";
$sql = "SELECT `status` FROM `table_name` WHERE `install_name`='$site_name';";
$result = msyql_query($sql);
$row = mysql_fetch_assoc($result);
$status = $row['status']; //if status = 1, then it's up, if it equals 0, then it's down
if ($status == 0){
$site_closed = true;
}
else{
$site_closed = false;
?>

if you insist upon doing that (I don't know WHO told you to do it this way... >.> <.<) then you could just

 

<?php
$maintPage = '<?php
define("MAINTENANCE", %s);
?>';
$maint = (isset($_POST['maint']) && ($_POST['maint'] == true)) ? 'true' : 'false';
file_put_contents('/path/to/my/file/check.php', sprintf($maintPage, $maint));
?>

No, no, jons.. your method is fine, I was giving an alternate method to using a database (I personally use a mixture of time range, cron jobs, and database for emergency maintenance). When I said "I don't know WHO told you to do it this way" I was referring to the op... and the fact that it was me that said it was a method he could use.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.