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
https://forums.phpfreaks.com/topic/62511-solved-session-var-problem-help/
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");
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.