php - session protection


I am creating a webpage bit by bit, testing parts of the webpage ideas. I want to learn how to session protect a page. I have already password protected a page seccsesfully, but anybody can access the page by typing in the url. i want to session protect my page so no one can do that. i have three pages: index.html, which has the form which sends the the password.php, the password.php, which makes sure that the password and username are correct using "if statments"(here is the "if statment")

    if ($username == 'mgmb99'){
    if ($password == 'mgmb91mas'){
    header('Location: youhere.php');
    } else {
    echo 'your username or password is wrong.<a href=""> go back to login page </a>';
    }} else {
    echo 'your username or password is wrong.<a href=""> go back to login page </a>';

, and the youhere.php which is the page once you logged in.





Sets the connect value in session to be 0.

Currently this check:


Will always return true because if$_SESSION['connect'] is unset then!$_SESSION['connect'] will be true. Likewiseif(!0) will be true.

Try setting$_SESSION['connect'] to true or 1 or the like or, alternatively, change the check to be:




( ! $_SESSION['connect'] ) will is true when the session variable isn't set but also when it is set to 0. So if you want to protect youhere.php, you need to assign another value and check for it.

Alsosession_destroy() will delete all session variables, so you login, you go to youhere.php but if you refresh the site, you will instantly be logged out




There is a plethora of information on Sessions on the PHP website.

Here's an example with storing and killing session variables.

To set a Session var:

if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {

To kill the session var:


People are also looking for solutions to the problem: php - Getting the image file into the file field while updating record


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.