skip to Main Content

Hi there I’m just trying to figure out how to check if a user is logged in and if so allow them to enter a page but if not simply return to ‘index.php’.

What I’ve basically done is create duplicate pages of such as ‘index.php’, ‘index2.php’, ‘service.php’ and ‘service2.php’ so on and so forth with minor adjustments such as no ‘search bar’.

Is it still possible to do so? I’d like the ‘Check to see if logged in’ on pages with ‘2’ in them. Such as ‘service2.php’ or ‘about2.php’

I’ve tried it with this code

(Keep in mind this is just the code not my whole entire page)

if(check_login()) {
  echo 'You are in!';
} else {
    header('Location: login.php');
    exit;
}

function check_login () {
    if(isset($_SESSION['login'] && $_SESSION['login'] != '') {
       return true;
    } else {
       false;
    }
}

Only used this code on pages that I want it to be displayed

Thanks in advance ! 🙂

UPDATE EDIT:

<?php
include ('config.php'); 

include ('function.php');

    $_SESSION['login']

?>
<!DOCTYPE HTML>
<html>
<head>
<title>Honda | </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='http://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="all" />
<!--slider-->
<link href="../css/camera.css" rel="stylesheet" type="text/css" media="all" />
    <script type='text/javascript' src='../js/jquery.min.js'></script>
    <script type='text/javascript' src='../js/jquery.mobile.customized.min.js'></script>
    <script type='text/javascript' src='../js/jquery.easing.1.3.js'></script> 
    <script type='text/javascript' src='../js/camera.min.js'></script> 

    <script>
        jQuery(function(){

            jQuery('#camera_wrap_1').camera({
                thumbnails: true
            });

            jQuery('#camera_wrap_2').camera({
                height: '400px',
                loader: 'bar',
                pagination: false,
                thumbnails: true
            });
        });
    </script>
</head>
<body>
<!--start header-->
<div class="h_bg">
<div class="wrap">
<div class="wrapper">
<div class="header">
    <div class="logo">
         <a href="index.php"><img src="../images/logo.png"> </a>
    </div>


    <div class="cssmenu">
    <ul>
       <li class="active"><a href="index.php"><span>Home</span></a></li>
          <li><a href="about.php"><span>About</span></a></li>
       <li class="has-sub"><a href="service.php"><span>Gallery</span></a>
       </li>
       <li class="last"><a href="contact.php"><span>Contact</span></a></li>
     <div class="clear"></div>
     </ul>
                     <div class="search">
                    <h2>search</h2>
            <form action="search.php" method="get">
                <input type="text" name="search" value="" placeholder="Enter Your search...">
                <input type="submit" name="submit" value="">
            </form>
        </div>

                 <div class="search1">

            <h2>login/Register</h2>

            <form action="" method="POST">

            <label>Username:</label>
                <input type="text" id="password" name="username" required />
            <label>Password:</label>
                <input type="password" id="password" name="password" required />
                <input type="submit" value="Login" name="submit" class="submit" />
                <br><br>
                <center>
                <h2><p><a href="register.php">Register</a></p></h2>
                </center>
    </form> 
    </div>







<?php
if(isset($_POST["submit"])){

if(!empty($_POST['username']) && !empty($_POST['password'])) {
    $username=$_POST['username'];
    $password=$_POST['password'];

    $password = strip_tags($password); 
$password = md5($password); // md5 is used to encrypt your password to make it more secure.

    $con=mysql_connect('localhost','root','') or die(mysql_error());
    mysql_select_db('aha') or die("cannot select DB");



    $query=mysql_query("SELECT * FROM login WHERE username='".$username."' AND password='".$password."'");
    $numrows=mysql_num_rows($query);
    if($numrows!=0)
    {
    while($row=mysql_fetch_assoc($query))
    {
    $dbusername=$row['username'];
    $dbpassword=$row['password'];
    }

    if($username == $dbusername && $password == $dbpassword)
    {
    session_start();
    $_SESSION['sess_user']=$username;

    /* Redirect browser */
    header("Location: member.php");
    }
    } else {
    echo "<div class='results'>Invalid username or password</div>";
    }

} else {
    echo "All fields are required!";
}
}

?>



    </div>

    <div class="clear"></div>
</div>
</div>
</div>
</div>
<!-- start slider -->
<div class="slider_bg">
<div class="wrap">
<div class="wrapper">
    <div class="slider">
    <!-- #camera_wrap_1 -->
    <div class="fluid_container">
        <div class="camera_wrap camera_azure_skin" id="camera_wrap_1">
            <div data-thumb="../images/thumbs/slider1.jpg" data-src="../images/slider/slider1.jpg">
            </div>
            <div data-thumb="../images/thumbs/slider2.jpg" data-src="../images/slider/slider2.jpg">
            </div>
            <div data-thumb="../images/thumbs/slider3.jpg" data-src="../images/slider/slider3.jpg">
            </div>
            <div data-thumb="../images/thumbs/slider4.jpg" data-src="../images/slider/slider4.jpg">
            </div>            
             </div><!-- #camera_wrap_1 -->
         <div class="clear"></div>
    </div>
    <!-- end #camera_wrap_1 -->
    <div class="clear"></div>
    </div>
</div>
</div>
</div>

<!-- start content -->
<div class="content_bg">
<div class="wrap">
<div class="wrapper">
    <div class="main">

    <!-- start content_right -->


                <div class="image group">
                <div class="grids_of_2">
                    <div class="grid images_3_of_1">
                        <img src="../images/pic1.jpg">
                    </div>
                    <div class="grid span_2_of_3">
                        <h3>How it all started</h3>
                        <p class="para">Honda Motor Co., Ltd. (本田技研工業株式会社 Honda Giken Kōgyō KK?, IPA: [hoɴda] ( listen); /ˈhɒndə/) is a Japanese public multinational corporation primarily known as a manufacturer of automobiles, motorcycles and power equipment.

Honda has been the world's largest motorcycle manufacturer since 1959,[3][4] as well as the world's largest manufacturer of internal combustion engines measured by volume, producing more than 14 million internal combustion engines each year.[5] Honda became the second-largest Japanese automobile manufacturer in 2001.[6][7] Honda was the eighth largest automobile manufacturer in the world behind General Motors, Volkswagen Group, Toyota, Hyundai Motor Group, Ford, Nissan, and PSA in 2011.[8]

Honda was the first Japanese automobile manufacturer to release a dedicated luxury brand, Acura, in 1986. Aside from their core automobile and motorcycle businesses, Honda also manufactures garden equipment, marine engines, personal watercraft and power generators, amongst others. Since 1986, Honda has been involved with artificial intelligence/robotics research and released their ASIMO robot in 2000. They have also ventured into aerospace with the establishment of GE Honda Aero Engines in 2004 and the Honda HA-420 HondaJet, which began production in 2012. Honda has three joint-ventures in China (Honda China, Dongfeng Honda, and Guangqi Honda).

In 2013, Honda invested about 5.7% (US$ 6.8 billion) of its revenues in research and development.[9] Also in 2013, Honda became the first Japanese automaker to be a net exporter from the United States, exporting 108,705 Honda and Acura models while importing only 88,357</p>
                   </div>
                    <div class="clear"></div>
                    </div>
                    <div class="grids_of_2 top">
                    <div class="grid images_3_of_1">
                        <img src="../images/pic2.jpg">
                    </div>
                    <div class="grid span_2_of_3">
                        <h3>CEO of Honda</h3>
                        <p class="para">Kawamoto acted quickly to change Honda's corporate culture, rushing through market-driven product development that resulted in recreational vehicles such as the Odyssey[disambiguation needed] and the CR-V, and a refocusing away from some of the numerous sedans and coupes that were popular with Honda's engineers but not with the buying public. The most shocking change to Honda came when Kawamoto ended Honda's successful participation in Formula One after the 1992 season, citing costs in light of the takeover threat from Mitsubishi as well as the desire to create a more environmentally-friendly company image.[21]

Later, 1995 gave rise to the Honda Aircraft Company with the goal of producing jet aircraft under Honda's name</p>
                   </div>
                    <div class="clear"></div>
                    </div>
                </div>
    </div>
    </center>
    <div class="clear"></div>
</div>
</div>
</div>
</div>
<div class="footer_bg">
<div class="wrap">
<div class="wrapper">
    <div class="footer">
        <div class="copy">
            <p class="w3-link">©&nbsp;</p>
        </div>
        <div class="f_nav">
        <ul>
            <li><a href="#">Skype</a></li>
            <li><a href="#">Linked in</a></li>
            <li><a href="#">Twitter</a></li>
            <li><a href="#">Facebook</a></li>
        </ul>
        </div>
        <div class="clear"></div>
    </div>
</div>
</div>
</div>
</body>
</html>



</body>
</html>

2

Answers


  1. The best experience in this case will be to put check_login into smth like functions.php and insert this in files where you want to use it:

    include "functions.php";
    if(!check_login()) {
        header('Location: login.php');
        exit();
    }
    

    You can also try using Apache’s native module mod_rewrite to handle large amount of such suffixed files.

    Login or Signup to reply.
  2. You are missing a paranthesis in the isset in the check login function, also the function could be made simpler.

    // I'd also put this function in some common file.
    function check_login() {
      return isset($_SESSION['login']) && $_SESSION['login'] != '';
    }
    
    // then in all other files you could include the common file, and do this
    if (!check_login()) {
      header('Location: login.php');
      exit;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search