I am hosting web site using a inex.php file. I have setup a login for four different users which currently directs them to same index.php file.
I would like direct each individual to a specifice index.php file based on the user. I am very new to php and coding in general. Could anyone hlep me with this?

Thank you in advance

Here is my code for the index.php

<?php

//======================================================================
// Positions pic
//======================================================================
?>

<style type="text/css"> /*Initializing CSS code*/
img { float: right; }
</style> 

<?php

//======================================================================
// Takes you to login
//======================================================================
?>

<?php include("/home/pi/html/password_protect.php"); ?>

<html>
<body>
<?php

//======================================================================
// creates logout
//======================================================================
?>

<a href="http://www.google.com/path/to/protected/page.php?logout=1">Logout</a>

<meta name="viewport" content="width=device-width, maximum-scale=1.0" />

<?php

//======================================================================
// Makes background color
//======================================================================
?>

<?php echo "<body style='background-color:purple'>"; ?>

<?php

//======================================================================
// Creates picture
//======================================================================
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title></title>
    </head>
    <body>
        <?php
        echo ("ENGR490");
        ?>
        <div>
            <img src="myPic.jpg" alt="myPic" />

        </div>
    </body>
</html>

<style>

   input[type=radio] {
    border: 1px;
    width: 4em;
    height: 4em;
}
   input[type=submit] {
   margin-left: 2em;    
}

</style>

    <?php
        if (isset($_POST['Submit_one'])){
            $on = "On";
            $off = "Off";
            $file = fopen("pump_one.txt","w+");

            //read in user fields (text box or radio button
            $radio_value_one = $_POST['on_off_one'];    

            //read in the radio buttons and write the status to file
            if ($radio_value_one == $on){
                fwrite($file, $on);
            }
            if ($radio_value_one == $off){
                fwrite($file, $off);
            }

            //close         
            fclose($file);                      
        }       
        $value_one = file_get_contents("pump_one.txt");

    ?>
    <?php
    $value_one = file_get_contents("pump_one.txt");
    ?>
    <br>
    <br>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method = "post">
    <input type="radio" name ="on_off_one"  <?php if ((isset($radio_value_one)) && ($radio_value_one==$on)) echo "checked";?> value = "On"> On  
    <input type="radio" name ="on_off_one" <?php if ((isset($radio_value_one)) && ($radio_value_one==$off)) echo "checked";?> value = "Off" > Off       
    <input type = "Submit" name ="Submit_one">
    </form>

    <h1>Tank one water request is... 
    <br>
    <b><?php echo $value_one ?></b></h1>

    <?php
        if (isset($_POST['Submit_two'])){
            $on = "On";
            $off = "Off";
            $file = fopen("pump_two.txt","w+");

            //read in user fields (text box or radio button
            $radio_value_two = $_POST['on_off_two'];    

            //read in the radio buttons and write the status to file
            if ($radio_value_two == $on){
                fwrite($file, $on);
            }
            if ($radio_value_two == $off){
                fwrite($file, $off);
            }

            //close         
            fclose($file);                      
        }       
        $value_two = file_get_contents("pump_two.txt");

    ?>
    <?php
    $value_two = file_get_contents("pump_two.txt");
    ?>
    <br>
    <br>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method = "post">
    <input type="radio" name ="on_off_two"  <?php if ((isset($radio_value_two)) && ($radio_value_two==$on)) echo "checked";?> value = "On"> On  
    <input type="radio" name ="on_off_two" <?php if ((isset($radio_value_two)) && ($radio_value_two==$off)) echo "checked";?> value = "Off" > Off       
    <input type = "Submit" name ="Submit_two">
    </form>

    <h1>Tank two water request is... 
    <br>
    <b><?php echo $value_two ?></b></h1>

    <?php
        if (isset($_POST['Submit_three'])){
            $on = "On";
            $off = "Off";
            $file = fopen("pump_three.txt","w+");

            //read in user fields (text box or radio button
            $radio_value_three = $_POST['on_off_three'];    

            //read in the radio buttons and write the status to file
            if ($radio_value_three == $on){
                fwrite($file, $on);
            }
            if ($radio_value_three == $off){
                fwrite($file, $off);
            }

            //close         
            fclose($file);                      
        }       
        $value_three = file_get_contents("pump_three.txt");

    ?>
    <?php
    $value_three = file_get_contents("pump_three.txt");
    ?>
    <br>
    <br>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method = "post">
    <input type="radio" name ="on_off_three"  <?php if ((isset($radio_value_three)) && ($radio_value_three==$on)) echo "checked";?> value = "On"> On    
    <input type="radio" name ="on_off_three" <?php if ((isset($radio_value_three)) && ($radio_value_three==$off)) echo "checked";?> value = "Off" > Off     
    <input type = "Submit" name ="Submit_three">
    </form>

    <h1>Tank three water request is... 
    <br>
    <b><?php echo $value_three ?></b></h1>

    <?php
        if (isset($_POST['Submit_four'])){
            $on = "On";
            $off = "Off";
            $file = fopen("pump_four.txt","w+");

            //read in user fields (text box or radio button
            $radio_value_four = $_POST['on_off_four'];  

            //read in the radio buttons and write the status to file
            if ($radio_value_four == $on){
                fwrite($file, $on);
            }
            if ($radio_value_four == $off){
                fwrite($file, $off);
            }

            //close         
            fclose($file);                      
        }       
        $value_four = file_get_contents("pump_four.txt");

    ?>  
    <?php
    $value_four = file_get_contents("pump_four.txt");
    ?>

    <br>
    <br>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method = "post">
    <input type="radio" name ="on_off_four"  <?php if ((isset($radio_value_four)) && ($radio_value_four==$on)) echo "checked";?> value = "On"> On   
    <input type="radio" name ="on_off_four" <?php if ((isset($radio_value_four)) && ($radio_value_four==$off)) echo "checked";?> value = "Off" > Off        
    <input type = "Submit" name ="Submit_four">
    </form>

    <h1>Tank four water request is... 
    <br>
    <b><?php echo $value_four ?></b></h1>
<?php
$page = $_SERVER['PHP_SELF'];
 $sec = "5";
 header("Refresh: $sec; url=$page");
 echo "Watch the page reload itself in 5 second!";
 ?>

</body>
</html>

Recommended Answers

All 5 Replies

Line 19 says <?php include("/home/pi/html/password_protect.php"); ?> but you aren't telling us what is in that file. If I understand you correctly, that file gets people to enter a username and password, and determines whether it's successful and whether they should be logged in or not?

Dani, here is the password_protect.php.

<?php

###############################################################
# Page Password Protect 2.13
###############################################################
# Visit http://www.zubrag.com/scripts/ for updates
############################################################### 
#
# Usage:
# Set usernames / passwords below between SETTINGS START and SETTINGS END.
# Open it in browser with "help" parameter to get the code
# to add to all files being protected. 
#    Example: password_protect.php?help
# Include protection string which it gave you into every file that needs to be protected
#
# Add following HTML code to your page where you want to have logout link
# <a href="http://www.example.com/path/to/protected/page.php?logout=1">Logout</a>
#
###############################################################

/*
-------------------------------------------------------------------
SAMPLE if you only want to request login and password on login form.
Each row represents different user.

$LOGIN_INFORMATION = array(
  'zubrag' => 'root',
  'test' => 'testpass',
  'admin' => 'passwd'
);

--------------------------------------------------------------------
SAMPLE if you only want to request only password on login form.
Note: only passwords are listed

$LOGIN_INFORMATION = array(
  'root',
  'testpass',
  'passwd'
);

--------------------------------------------------------------------
*/

##################################################################
#  SETTINGS START
##################################################################

// Add login/password pairs below, like described above
// NOTE: all rows except last must have comma "," at the end of line
$LOGIN_INFORMATION = array(
  'zubrag' => 'root',
  'admin' => 'adminpass'
);

// request login? true - show login and password boxes, false - password box only
define('USE_USERNAME', true);

// User will be redirected to this page after logout
define('LOGOUT_URL', 'http://www.example.com/');

// time out after NN minutes of inactivity. Set to 0 to not timeout
define('TIMEOUT_MINUTES', 0);

// This parameter is only useful when TIMEOUT_MINUTES is not zero
// true - timeout time from last activity, false - timeout time from login
define('TIMEOUT_CHECK_ACTIVITY', true);

##################################################################
#  SETTINGS END
##################################################################

///////////////////////////////////////////////////////
// do not change code below
///////////////////////////////////////////////////////

// show usage example
if(isset($_GET['help'])) {
  die('Include following code into every page you would like to protect, at the very beginning (first line):<br>&lt;?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?&gt;');
}

// timeout in seconds
$timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);

// logout?
if(isset($_GET['logout'])) {
  setcookie("verify", '', $timeout, '/'); // clear password;
  header('Location: ' . LOGOUT_URL);
  exit();
}

if(!function_exists('showLoginPasswordProtect')) {

// show login form
function showLoginPasswordProtect($error_msg) {
?>
<html>
<head>
  <title>Please enter password to access this page</title>
  <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
  <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</head>
<body>
  <style>
    input { border: 1px solid black; }
  </style>
  <div style="width:500px; margin-left:auto; margin-right:auto; text-align:center">
  <form method="post">
    <h3>Please enter password to access this page</h3>
    <font color="red"><?php echo $error_msg; ?></font><br />
<?php if (USE_USERNAME) echo 'Login:<br /><input type="input" name="access_login" /><br />Password:<br />'; ?>
    <input type="password" name="access_password" /><p></p><input type="submit" name="Submit" value="Submit" />
  </form>
  <br />
  <a style="font-size:9px; color: #B0B0B0; font-family: Verdana, Arial;" href="http://www.zubrag.com/scripts/password-protect.php" title="Download Password Protector">Powered by Password Protect</a>
  </div>
</body>
</html>

<?php
  // stop at this point
  die();
}
}

// user provided password
if (isset($_POST['access_password'])) {

  $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
  $pass = $_POST['access_password'];
  if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION)
  || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ) ) 
  ) {
    showLoginPasswordProtect("Incorrect password.");
  }
  else {
    // set cookie if password was validated
    setcookie("verify", md5($login.'%'.$pass), $timeout, '/');

    // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
    // So need to clear password protector variables
    unset($_POST['access_login']);
    unset($_POST['access_password']);
    unset($_POST['Submit']);
  }

}

else {

  // check if password cookie is set
  if (!isset($_COOKIE['verify'])) {
    showLoginPasswordProtect("");
  }

  // check if cookie is good
  $found = false;
  foreach($LOGIN_INFORMATION as $key=>$val) {
    $lp = (USE_USERNAME ? $key : '') .'%'.$val;
    if ($_COOKIE['verify'] == md5($lp)) {
      $found = true;
      // prolong timeout
      if (TIMEOUT_CHECK_ACTIVITY) {
        setcookie("verify", md5($lp), $timeout, '/');
      }
      break;
    }
  }
  if (!$found) {
    showLoginPasswordProtect("");
  }

}

?>
Member Avatar for diafol

Confused as to what you need help with. Do you need help with designing a system to redirect logged in users to a specific page (different url) or just show different info (targeted) on the same page? Where's the issue, is there is one?

Sorry haven't scanned the code, just want elucidation.

I want to redirect on the same website but a different page or portion of a page.

The way my page is setup now each user goes to the same index.php file.
I want to seperate the "pumps" on my page into four seperate pages that are dedicated to 1 user per page. So user1 logins in and they are directed to and index1.php file that only has pump1 etc.

Thank you

Member Avatar for diafol

I would suggest that you keep the same index.php page for all users. Otherwise, you'll end up creating bespoke pages for all new users, and that's a maintenance nightmare. You can keep track of the logged in user with a $_SESSION variable and apply specific include files / structures accordingly. If you must have separate urls for all, you can use a rewrite giving the url something like index.php/345 where the number relates to the member id. Of course, this needs to be read and checked against the $_SESSION id to prevent anybody from randomly getting another user's info. However, I can't really see a case for this.

Your members should have a some data in your DB, which specifies which bits of data / structure info ("pump") to include on the index.php page. However, the "pumps" could also be stored in files and displayed that way. If you are storing info in text files, ensure that they are protected (above docroot) from casual perusal via url. My 2p.

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.