Jump to content

Recommended Posts

I need help with a protected php scripts. THis script should go above every member pages after login. I try to make one, but instead of go to 1st page protected it goes to login.php need help

 

<?php

session_start();

require_once("connect.php");

if(!isset($_SESSION["username"]))

{

  header('Location: login.php');

  exit;

}

?>

Link to comment
https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/
Share on other sites

posting your login script would help, but make sure of a few things

 

1. that the session 'username' is the correct name of the session variable you set when a user logs in

2. that you actually set the username to a value. this is a very common problem

Hi,

 

im posting the login code.

<?php

// we must never forget to start the session

session_start();

 

if (isset($_POST['username']) && isset($_POST['password'])) {

     

  $username = mysql_real_escape_string($_POST['username']);

$password = mysql_real_escape_string($_POST['password']);

 

require_once("connect.php");

 

  // check if the user id and password combination exist in database

  $query = "SELECT username

          FROM members

          WHERE username = '$username'

                AND password = '$password'";

 

  $row = mysql_query($query)

  or die ("Error - Couldn't login user."); 

 

  if (mysql_num_rows($row) == 1) {

      // the user id and password match,

      // set the session

      $_SESSION[username] = $row[username];

 

      // after login we move to the main page

      echo "Welcome $username! You've been successfully logged in."; 

exit();

 

  } else // bad info.

  {

      echo "Error - Couldn't login user.<br /><br />

        Please try again.";

      exit();

  }

 

}

?>

  $_SESSION[username] = $row[username];

 

this is your problem. the $row variable holds a mysql resource, not a mysql associative array. to do that you need to do

 

$array = mysql_fetch_assoc($row);
$_SESSION['username'] = $array['username'];

 

hope that helps

the logic is that the session var isn't set to anything, because what you are trying to set it to doesn't exist. I'm suprised you don't get a syntax error on your log in page.

session_start();
require_once("connect.php");
if(!isset($_SESSION["username"]))
{
   header('Location: login.php');
   exit;
}
?>

 

this is perfectly fine, the problem lies in your login script:

 

<?php
// we must never forget to start the session
session_start();

if (isset($_POST['username']) && isset($_POST['password'])) {
            
   $username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

require_once("connect.php");

   // check if the user id and password combination exist in database
   $query = "SELECT username
           FROM members
           WHERE username = '$username'
                 AND password = '$password'";

   $row = mysql_query($query)
   or die ("Error - Couldn't login user."); 

   if (mysql_num_rows($row) == 1) {
      // the user id and password match,
      // set the session
      $_SESSION[username] = $row[username];

      // after login we move to the main page
      echo "Welcome $username! You've been successfully logged in."; 
exit();

   } else // bad info.
   {
      echo "Error - Couldn't login user.<br /><br />
         Please try again.";
      exit();
   }

}
?>

 

I don't really know what your saying at all... but you clearly don't understand session variables if you are having trouble understanding what the problem is with your script (especially since we explained it) OR you don't understand english very well (as evident by your speech) and can't really understand what i'm saying. Either try reading a tutorial on session variables and mysql results, or try a different, non-english board. best of luck

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.