Lee26860
Lee26860

Reputation: 1

Password protect for specific pages

I've been making an educational website in PHP, and I've decided to password-protect pages to the public unless they're signed in - in this case, students.

The subjects covered are GCSE Maths, English and History, and I want to lock the pages via password.

Options I looked at were:

.htpassword = Not relevant here

Various password protection scripts = they could lock the pages, but people could view all pages - not much use, when, say I want people to enter a DIFFERENT password for DIFFERENT webpages (e.g. you visit the page on GCSE English literature, you use a separate password once logged-in to view it, you visit the page on GCSE Maths algebra, you would enter a different password once logged-in to view it.)

User registration isn't an issue, as because they're students, I'm creating the users and passwords - preventing useless registrations etc.

It's not how to password-protect that's the main issue, it's how to use a different password on every protected page in addition to the user login, so what would you suggest?

I've done my research, but am not sure where to go next with this, so where should I progress from here?

Sorry if it seems complex, it's just this issue is fairly important for me and none of the scripts I found online seemed to fit this, and my PHP coding skills are good for a beginner, but not in the area of securing files in PHP.

(I'd considered a CMS but since the site is a PHP adaptation from former HTML pages, it didn't make sense for the audience it was aimed at - it is an educational site, not a business site, and the website itself is moving servers anyway to a new Apache one.)

Upvotes: 0

Views: 779

Answers (3)

Jack Billy
Jack Billy

Reputation: 7211

You can do this with the session in PHP. Here is a full explanation of session. But here is a code for your site :

<?php
  if(isset($_SESSION['USER_NAME'])) //Checking if the session is available or not.
  {
      echo 'Private Content!'; //Show the private content.
  }
  else
  {
      echo 'You have no rights to view this page!'; //Error message.
  }
?>

And you can use MySql to store your passwords and username in it. Or can just create an array (to store data) file and a login (to process login) file in your server.

But I recommend you to create a well developed user based system for your users and give some specials offers for registered users.

Hope this helps.

Upvotes: 0

Repox
Repox

Reputation: 15476

While actually registering your users, why not give your users permissions instead of extra passwords? That would be more efficient and add flexibility to your solution - in case you add extra material for your students.

It would only require an extra database table to link your users to the materials.

Upvotes: 1

Gerben Jacobs
Gerben Jacobs

Reputation: 4583

The most basic thing I can come up with, is just make a PHP file with arrays that contain pages, usernames and passwords.

<?php
$protected = array(
    'history.php' => array(
        'mike'  => 'pswrd',
        'ryan'  => 'pswrd',
    ),
    'maths.php' => array(
        'mike'  => 'pswrd',
        'ryan'  => 'pswrd',
    ),
);
?>

And then check on your page for this. Maybe through $_GET or make a simple form with POST.

Upvotes: 0

Related Questions