Jump to content

[SOLVED] SESSION var problem - HELP


SilentQ-noob-

Recommended Posts

I want to password protect a portfolio page on my website, but its not working out. I want to set a session variable to 0, then redirect to login if it does  = 0. On the login page, once the password is entered, I set the variable to 1 and redirect back to portfolio- but it doesnt work. When I run the script with this code:

portfolio.php

<?php
session_start();
$_SESSION['password'] = 0;
  if($_SESSION['password'] = 0) {
  header("location: login.php");
}
?>

 

and this code on login.php

<?php
session_start();
if(isset($_POST['password'])) {
  if ($_POST['password'] == "example") { 
   $_SESSION['password'] = 1; 
   echo "Login Successful!" ;
header("location: portfolio.php");
  exit;
  }
}
?>

When I click on "portfolio" in the menu bar, it goes right to portfolio.php, and not to login

Link to comment
Share on other sites

Well, firstly you're using the assignment operator inside your if statement (single equals sign), which is always going to evaluate to true. However, you're also setting the value of $_SESSION['password'] to 0 right before you check it, so its never going to work. This should do the trick:

 

<?php
session_start();
  if($_SESSION['password'] == 0) {
  header("location: login.php");
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.