简体   繁体   中英

How to turn on/off a php file while in admin page?

Using the Admin page, I want to toggle on/off or enable/disable a php file and I'm using sessions but the session stops as I close the browser. I want to keep the session active even if I close the browser. Am I using the right code? I think it's not session...?

<form method="post" action="">
    <td colspan="2"  bgcolor="#FFFFFF" valign="left">
        <strong>APPLICATION FORM:
        <select name="switch" id="switch" align="center" valign="center">
            <option></option>
            <option>ON</option>
            <option>OFF</option>
        </strong>
        </SELECT>

        <input type="submit" name="submit" value="SUBMIT">

        <?php
        if(isset($_POST['submit'])){
            include('config.php');
            $switch=($_POST['switch']);

            if ($_POST['switch'] == "ON"){
                $_SESSION['switch'] = $switch;
                echo "<script language='javascript' type='text/javascript'>";
                echo "alert('Application form ACTIVATED!');";
                echo "</script>";
                echo 'ON';  
            }

            if ($_POST['switch'] == "OFF"){
                echo "<script language='javascript' type='text/javascript'>";
                echo "alert('Application form DEACTIVATED!');";
                echo "</script>";
                echo 'OFF'; 

                session_destroy();
                header('location.href=index.php');
            }
        }
        ?>
    </td>
</form>

<?php
session_start();
?>

<?php
$con    =   mysql_connect('localhost','root','');
if (!$con) {
    die('Could not connect:'.mysql_error());
}

mysql_select_db ('psp',$con);

if (!isset($_SESSION['switch'])){
    header('location:index.php');
}
?>

You can turn off/on a page using database or file in saving the settings..

yes, sessions can be used to disable/enable php files, but it would reset on browser exit. While cookies can be useful, the settings will reset when you clear browser cookies or if the cookies expires..

the best approach would be using database or a file in saving the settings..

so, you can create a table in your database with columns:

select * from file_settings;
+-------+-------------+-----------+
| id    |  filename   |   status  |
+-------+-------------+-----------+
| 0     |  file.php   |    ON     |
+-------+-------------+-----------+

then on your file.php :

<?php
  /* code for getting the columns in file_settings:
     lets make a shortcut, just query the database and
     it will output every row in the table as $row
  */
  if("file.php" == $row['filename'] && $row['status'] != "ON") {
    header('the_redirect_page.php');
  } 
  // if false then proceed to page
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM