0

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');
}
?>
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
Erin Alde
  • 17
  • 3
  • 1
    http://stackoverflow.com/questions/3684620/is-possible-to-keep-session-even-after-the-browser-is-closed – Vertig0 May 20 '15 at 03:34
  • 1
    save data instead of session save to database or file or cookie – ashkufaraz May 20 '15 at 03:34
  • in addition to @ashkufaraz's answer, session's lifetime ends at browser exit or if the program unsets it, while you can set a lifetime of a cookie but it can be removed in the browser when you clear it's browser cache and cookies... file and database would be good since it can be saved to the server side and settings can remain unchanged. – catzilla May 20 '15 at 03:52
  • How will it turn on/off a certain php file if I'd save the data? – Erin Alde May 20 '15 at 03:52
  • use mysql database. create a table called page and give a column name "status". when you click off, update table status to "off" or reverse. – Elyor May 20 '15 at 03:52
  • Hi, have a look at this page: http://fi.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime – Sourabh Kumar Sharma May 20 '15 at 03:58
  • @ErinAlde you can turn off/on the file through updating the tables in the database.. – catzilla May 20 '15 at 04:07
  • Oh. Thank you guys! Such a big help. – Erin Alde May 20 '15 at 04:08
  • Sidenote: you're outputting before header – Funk Forty Niner May 20 '15 at 04:52

1 Answers1

2

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
?>
catzilla
  • 1,901
  • 18
  • 31