Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

help required with small php script

Featured Replies

Hi guys

 

I need a little help again.

 

I am using this very simple php script to password protect content on a website.

 

<?php $password = "frog";?>

<?php 
// If password is valid let the user get access
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
?>
<!-- START OF HIDDEN HTML -->

 <p> </p>

<!-- END OF HIDDEN HTML -->
<?php 
}
else
{
// Wrong password or no password entered display this message
if (isset($_POST['password']) || $password == "") {
 print "<h4>Incorrect Password</h4><h4>Please enter the correct password</h4>";}
 print "<form method=\"post\" action=\"\"><p>Please enter your password for access<br />";
 print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\" /><input value=\"Login\" type=\"submit\" /></p></form>";
}
?>

 

Live example here

 

On one of the sites I am using this script, the owner now want it so that if you navigate away from the page, pressing the back button will require re-entering the password. In addition they would like a logout button.

 

I know almost nothing about php and would really be most appreciative if someone could help me adapt this script to accommodate their needs.

 

Many thanks

Cara

Add this to the top of the page

 

<?php
session_start();
?>

 

Also you could add this:

 

<?php

if (!isset($_SESSION["auth_username"])) {
echo "You must be logged in to view this page";
exit;
}
else {

echo "";
}
?>

 

the auth_username you can change to auth_password but use this line of code too:

 

To use logout, just create a form with a button instead like so:

<form action="logout.php" method="post">
	<input type="submit" name="sent" id="sent" value="Logout" /></form>

 

then in the logout.php file you just put the following code:

<?php session_start();

session_destroy();

header("Location: success.php");
//This is the location where you want the user to go after the session has been destroyed
?>

 

Hope this helps

  • Author

Add this to the top of the page

 

<?php
session_start();
?>

 

Also you could add this:

 

<?php

if (!isset($_SESSION["auth_username"])) {
echo "You must be logged in to view this page";
exit;
}
else {

echo "";
}
?>

 

the auth_username you can change to auth_password but use this line of code too:

 

To use logout, just create a form with a button instead like so:

<form action="logout.php" method="post">
	<input type="submit" name="sent" id="sent" value="Logout" /></form>

 

then in the logout.php file you just put the following code:

<?php session_start();

session_destroy();

header("Location: success.php");
//This is the location where you want the user to go after the session has been destroyed
?>

 

Hope this helps

Hi lstables

Thanks. I can't seem to get it to work as the back button still displays password protected content :sorry:

Im a bit confused by:

 

// Wrong password or no password entered display this message
if (isset($_POST['password']) || $password == "") {

 

If password isset or password == ""

 

Incurrect login message.... everytime some one trys to login they will get that message

  • Author

Im a bit confused by:

 

// Wrong password or no password entered display this message
if (isset($_POST['password']) || $password == "") {

 

If password isset or password == ""

 

Incurrect login message.... everytime some one trys to login they will get that message

Have no idea how it works, just know that it does!

I'm not sure then, I still learning PHP and all the above works for me maybe you show us the full code ?!?! If not then am sure someone on here will help, there are a lot of knowledgable people on here...

  • Author

I'm not sure then, I still learning PHP and all the above works for me maybe you show us the full code ?!?! If not then am sure someone on here will help, there are a lot of knowledgable people on here...

 

 

I have given you the fill code. Thanks anyways, hopefully someone else will have the answer.

Im a bit confused by:

 

// Wrong password or no password entered display this message
if (isset($_POST['password']) || $password == "") {

 

If password isset or password == ""

 

Incurrect login message.... everytime some one trys to login they will get that message

 

It works because it's in a different section of the "if" statement. "If login password is correct then do x" else "if login password is set or blank then do x".

 

The second part of that statement is only active if the password is wrong. So it doesn't matter. It's poor coding but it works.

 

It should be:

 

// Wrong password or no password entered display this message
if (!isset($_POST['password']) || $password == "") {

 

If password is not set or password is blank. You could further cut it down to just:

 

// Wrong password or no password entered display this message
if (!isset($_POST['password'])) {

 

I can post the answer later don't have time right now I need to go out.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.