Hello
I want to have 4 files
index.php
go1.php
go2.php
admin.php

in admin.php I want a switch command to switch between go1.php and go2.php
for example in admin.php there must be two buttons go1 and go2 when I click on go1 index.php will behave as go1.php and when I click on go2 index.php will behave as go2.php

any tutorial or easy way to do this ?

Recommended Answers

All 6 Replies

Include the files????

if($some_kind_of_variable == 'some_value')
    include('go1.php');
else
    include('go2.php');

I don't really understand what you want to do.

admin.php needs to be connected with index.php
in admin.php when I click on go1 button or option index.php will show the contents of go1.php otherwise it will show the contents of go2.php

There are multiple ways of doing this. You could use MySQL, but I am going to write the info to a seperate file. The code I am posting is an example, I wouldn't use it for a large website and it is probably not the recommended way of doing it...

admin.php

<?php
// ...
if(isset($_POST['page_option']))
{
    $fp = fopen('data.txt', 'w');

    if($_POST['page_option'] == 'go1')
        $write = '1';
    else if($_POST['page_option'] == 'go2')
        $write = '2';

    fwrite($fp, $write);
    fclose($fp);
}
?>
<form action="" method="post">
 <select name="page_option">
  <option value="go1">Go 1</option>
  <option value="go2">Go 2</option>
 </select>
 <input type="submit" value="Change index.php" />
</form>

Then in index.php

<?php
$fp = fopen('data.txt', 'r');
$contents = fread($fp, filesize('data.txt'));
fclose($fp);

if($contents == '1')
    include('go1.php');
else if($contents == '2')
    include('go2.php');
else
    echo 'Something else...';
?>

The data.txt file should contain the number 1 or 2. If its 1 then on index.php, go1.php is included, if it is 2 then go2.php in included.

I think this answers your question. I am sure there Are better ways of doing this, I'm sure you can explore alternatives.

---
Kieran

commented: Nice it works +0

Yes it works
Many Thanks
What if I want to protect the admin.php with username and pass to not make it publicly accesible

You could, again, use MySQL but if you are using Apache (or a package with Apache in it) you could use .htaccess and .htpasswd

See this website: http://www.elated.com/articles/password-protecting-your-pages-with-htaccess/ - it explains how to use that method.

Another way, which isn't the best or most secure, you could change admin.php to:

<?php
    // ...
    if(isset($_POST['page_option']))
    {
        if(isset($_POST['password']) && strcmp($_POST['password'], 'your_password_here') == 0)
        {
            $fp = fopen('data.txt', 'w');

            if($_POST['page_option'] == 'go1')
                $write = '1';
            else if($_POST['page_option'] == 'go2')
                $write = '2';

            fwrite($fp, $write);
            fclose($fp);
        }
        else
        {
            echo '<p>You must enter the correct password to perform that action.</p>';
        }
    }
    ?>
    <form action="" method="post">
     <select name="page_option">
      <option value="go1">Go 1</option>
      <option value="go2">Go 2</option>
     </select>
     <br />
     <input type="password" name="password" />
     <br />
     <input type="submit" value="Change index.php" />
    </form>

Just change your_password_here to any value you want to use for a password.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.