adamjones Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 22, 2008 Share Posted September 22, 2008 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 } ?> Quote Link to comment Share on other sites More sharing options...
Jamrams Posted September 22, 2008 Share Posted September 22, 2008 Yeah, I'd like to know too, but I don't understand your code, could you explain it please? Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 22, 2008 Share Posted September 22, 2008 Not enough... If the 'maintenance' setting is to be site-wide, then it should be written to a file, or to a database. Session will keep it for one user only. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
Jamrams Posted September 22, 2008 Share Posted September 22, 2008 Right I see... but how would you do it so that everyone would see the maintenance page? Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
adamjones Posted September 22, 2008 Author Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 22, 2008 Share Posted September 22, 2008 wow, unless you are doing this very often, why not just do it manually. really? If you have a db, I guess you might as well set up the button, but really? why? simple change of a string value in text... done. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 22, 2008 Share Posted September 22, 2008 Functions you'll need are fopen(), fclose(), fwrite(), and fread(). Look them up in manual and I'm sure you can come up with something. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 22, 2008 Share Posted September 22, 2008 thank you mchl... just incase adamjones doesn't know where to look: http://us.php.net/fopen Quote Link to comment Share on other sites More sharing options...
jonsjava Posted September 22, 2008 Share Posted September 22, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 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)); ?> Quote Link to comment Share on other sites More sharing options...
jonsjava Posted September 22, 2008 Share Posted September 22, 2008 actually, my way works quite nicely. Never had an issue from it to date. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted September 22, 2008 Share Posted September 22, 2008 lol, ok. misunderstanding. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 I phrased poorly I should have quoted. My bad. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.