Search code examples
phpindexingdirectorydefault

How do I make a default page for my website application with php?


So I wish someone could give me a simple function or solution to put a default page on my site. That mean if the user try to access another page of my site he can't. He need to pass by this default page. Because in my site I have an admin page and I don't want a simple user to access it . I did some research on internet and I didn't find a good answer to my problem. Is there any way to do it simply by code and not changing the index file? To be more specific I want it to work only for one of my website and not for all . I have a login system for admin but since you can access the admin page directly by typing the address and not using the login system it's a problem. I did try to do an index but the problem is when I try to log in then it redirect me on the default page and not on the admin page. I will appreciate any help. Best Regards a young programmer.

Here is the index.php

<?php

header('Location: Foredeckmain.php');
exit;
?>

Here is the login page

<?php
   ob_start();
   session_start();
?> 

<html lang = "en">

   <head>
      <title>Foredeck Login.com</title>
      <link href = "css/bootstrap.min.css" rel = "stylesheet">

      <style>
         body {
            padding-top: 40px;
            padding-bottom: 40px;
            background-color: #6495ED;
         }

         .form-signin {
            max-width: 330px;
            padding: 15px;
            margin: 0 auto;
            color: #000000;
         }

         .form-signin .form-signin-heading,
         .form-signin .checkbox {
            margin-bottom: 10px;
         }

         .form-signin .checkbox {
            font-weight: normal;
         }

         .form-signin .form-control {
            position: relative;
            height: auto;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding: 10px;
            font-size: 16px;
         }

         .form-signin .form-control:focus {
            z-index: 2;
         }

         .form-signin input[type="text"] {
            margin-bottom: 0 px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
            border-color:#000000;
         }

         .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
            border-color:#000000;
         }

         h3{
            text-align: ;
            color: #000000;
         }
         h1{
            text-align: ;
            color: #000000;
         }
      </style>

   </head>

   <?php
        $msg = '';

        if (isset($_POST['login']) && !empty($_POST['username']) 
            && !empty($_POST['password'])) {

            if ($_POST['username'] == 'foredeck' && 
              $_POST['password'] == 'foredeck1') {
              $_SESSION['valid'] = true;
              $_SESSION['timeout'] = time();
              $_SESSION['username'] = 'foredeck';
              $msg ='Connexion Réussite';
              echo "<script type='text/javascript'>alert('Connexion Réussite');
                window.location='foredeck.php'; </script>";

              header("refresh:3 location: foredeck.php");

            }else {
              $msg='Identifiant ou Mot de Passe incorrecte';
              $msg =  "<script type='text/javascript'>alert('$msg')</script>";
               }
            }
     ?>
  </div> <!-- /container -->

  <div class = "container">

     <form class = "form-signin" role = "form" 
        action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); 
        ?>" method = "post">
        <h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
        <h1>Foredeck@Admin</h1> 
        <h3>Entrer l'identifant et le mot de passe:</h3> 
        <input type = "text" class = "form-control" 
               name = "username" placeholder = "Identifiant " 
               required autofocus></br>
        <input type = "password" class = "form-control"
               name = "password" placeholder = "Mot de passe  " required>
        <br>

        <button class = "btn btn-lg btn-primary btn-block" type = "submit" 
               name = "login">Se connecter</button>
     </form>
  </div> 
  </body>

Here is the logout part

<?php
   session_start();
   unset($_SESSION["username"]);
   unset($_SESSION["password"]);

   echo 'Déconnexion en cours...';
   header('Refresh: 2; URL = foredeckmain.php');
?>

Solution

  • To be true, I didn't understand a reason why you want to do it, but anyway.

    But if I were you, I would make next steps

    1. Configure Apache/Nginx/or what you're using for your website/ to process all your request through your default page(index.php or something like that)

    2. In index.php you need to understand what page your user needs. If it doesn't exist show him 404 page.

    3. If page exists and it's not your default page, and user didn't visit it before, redirect him there.
    4. In your default page, write to session e.g $_SESSION['passed_default'] = 1;

    5. Now, in your index.php you need to check

      if(isset($_SESSION['passed_default']) && $_SESSION['passed_default']) { //Show desired page } else { //redirect to default }