PHP - allow only one area of the website to the user

Asked

Viewed 30 times

0

I have a search bar that only registered people can access. So far so good my code is working, the problem with every search in the search bar credentials are requested, IE, my code always requires you to login. but what I want is that only ask to log in when the person has logged in.

My code:

<?php

include("config.php");

    if(!isset($_SESSION['user'])){
        echo "you are not logged in,please click here to <a href='memberarea.html'>Login</a>";
    } else{

 $query = $_GET['query']; 


    $min_length = 3;


    if(strlen($query) >= $min_length){ 

        $query = htmlspecialchars($query); 


        $query = mysqli_real_escape_string($conn,$query);

      $row_results = mysqli_query($conn,"SELECT * FROM books WHERE `Title` LIKE '%".$query."%' OR `category` LIKE '%".$query."%'") or die(mysqli_error($conn));


        if(mysqli_num_rows($row_results) > 0){ 

            while($results = mysqli_fetch_array($row_results)){

                echo "<p><h3>".$results['Title']."</h3>".$results['category']."</p>";

            }

        }
        else{ 
            echo "No results";
        }

    }
    else{ 
        echo "Minimum length is ".$min_length;
    }
    }
?>
  • In the question, the credentials are requested or lost? You’re calling session_start() at the beginning of the code?

2 answers

1

when I used this in php I did so on all pages

if (!isset($_SESSION["logado"])) {
   $_SESSION["logado"] = false;
   session_start();
}

0

To start a Session, we use the session_start() function. For a good functioning, it cannot be after any data OUTPUT (echo, print, HTML codes, etc.). It is recommended to be in the first line of the code.

If in your include("config.php"); session_start() was not called

 <?php
 session_start();

 include("config.php");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.