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.

PHP logon script help!

Featured Replies

Hi, Im fairly new to this forum so any help would be greatly appreciated. I shall post an introduction later once i am outside of work.

 

Basically i have a PHP script to allow users to log in with a username and password. So far the script for logging in is on the same page as the content i am trying to display. I also have another 2 pages that follow the same structure as the one with the content.


My problem is that i can log in on the 1st page and everything displays fine including the 2nd and 3rd pages. But once i click back on to the 1st page it requires a username and password to be entered again.

 

How can i get the webpage/php script to recognise the user has already inputted their details?

 

I am using this code at the top of the page but i dont think it is correct:- <?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?>


The question at hand is, How can i make it so it recognises the password without using a database/MySQL?

 

I am reletively new to PHP, well and HTML to be honest but so far i have made excellent progress, i just cant fathom out this last part.


Thanks in advance and Kind Regards,
Ben.

Could you post some code snippets? Unfortunately you'll have to remove any URLs due to a limitation of your new account here - so apologies for that nuisance.

 

I assume you're using some kind of (session) cookie / MySQL table to store session data?

You will need to use cookies or sessions to communicate the state of logged in or not between the pages. If you're not familiar with cookies and sessions do a google search and you'll find plenty of info. You'll find its pretty simple to implement.

  • Author

Hi Andy9l, Cheers for the quick reply!

 

Please find below the full code minus any links/passwords etc. I'm trying to use cookies as MySQL seems more of a task to get my head around compared to php/cookies.

 

 

<?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?>
<?php require('../../require/....'); ?>
<?php require('../../require/.....'); ?>
<?php require('../../require/.....'); ?>

<?php



$LOGIN_INFORMATION = array(
  '************************************'
);

error_reporting(E_ALL);
ini_set('display_errors',1);

define('USE_USERNAME', true);


define('LOGOUT_URL', '**************');


define('TIMEOUT_MINUTES', 0);


define('TIMEOUT_CHECK_ACTIVITY', true);


if(isset($_GET['help'])) {
  die('Include following code into every page you would like to protect, at the very beginning (first line):<br><?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?>');
}

$timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);

if(isset($_GET['logout'])) {
  setcookie("verify", '', $timeout, '/');
  header('Location: ' . LOGOUT_URL);
  exit();
}

if(!function_exists('showLoginPasswordProtect')) {

function showLoginPasswordProtect($error_msg) {
?>
<html>
<head>
  <font color="white"><title>Please enter password to access this page</title></font>
  <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
  <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<body>
  <style>
    input { border: 1px solid black; }
  </style>
  <div style="width:500px; margin-left:auto; margin-right:auto; text-align:center">
  <form method="post">
   <font color="white"> <h3>Please enter password to access this page</h3></font>
    <font color="red"><?php echo $error_msg; ?></font><br />
<font color="white"><?php if (USE_USERNAME) echo 'Login:<br /><input type="input" name="access_login" /><br />Password:<br />'; ?></font>
   <font color="white"> <input type="password" name="access_password" /><p></p><input type="submit" name="Submit" value="Submit" /></font>
  </form>


 

 

Thanks and Kind Regards,

Ben.

Edited by rallport
Pleas euse code tags in the future, you'll get a better response when people can read your code :)

That is some crazy login script! Is that all of the code? I can't see any code to handle the actual login itself?

That is some crazy login script! Is that all of the code? I can't see any code to handle the actual login itself?

This is what i find crazy

 

 <font color="white"><title>Please enter password to access this page</title></font>
 

 

ive never seen this before lol

Edited by webdesigner93

Your form doesn't have an action so no processing of the form will ever occur.

<form action="#" method ="post"> 

The action field tells the browser which script will process the form - in the above instance, the current script will handle the form. Because the method is set to 'post', all the form variables can be accessed via the $_POST global variable.

 

You'd do best to look at some basic tutorials for forms - html and php and css. Learn about separating markup from styling. Not only will you get a lot more help because your code will be much more readable, but because it is the right thing to do on so many levels.

This might be a usefull link. Adam Khory at developphp.com have several tutorials on secure php logins you can grab the code from the page and watch video tutorials where he explain every detail. Its all modern, modular, semantic and best of all it's FREE.

http://www.developphp.com/view.php?tid=762

This might be a usefull link. Adam Khory at developphp.com have several tutorials on secure php logins you can grab the code from the page and watch video tutorials where he explain every detail. Its all modern, modular, semantic and best of all it's FREE.

http://www.developphp.com/view.php?tid=762

i wouldnt really suggest his tutorial all that much i mean its better then the above code the OP wrote but it also uses very outdated methods such as

ereg_replace("[^0-9]", "", $_GET['id']);

which ereg_replace is deprecated he also uses the mysql set of functions when he should use the mysqli set or PDO

i wouldnt really suggest his tutorial all that much i mean its better then the above code the OP wrote but it also uses very outdated methods such as

ereg_replace("[^0-9]", "", $_GET['id']);

which ereg_replace is deprecated he also uses the mysql set of functions when he should use the mysqli set or PDO

Notice taken. I'll be cafull with recomending him in the future. I'll also admit that some of his tuts are very old. The reason why I recomend this resource is because I really think mr. Khory is a good at explaining code in a way thats easy to understand for beginners.

Edited by Nillervision

Notice taken. I'll be cafull with recomending him in the future. I'll also admit that some of his tuts are very old. The reason why I recomend this resource is because I really think mr. Khory is a good at explaining code in a way thats easy to understand for beginners.

yes hes very good at explaining code :) as long as u replace his methods with updated ones i think it will be ok id just warn users when u did recommend him thats all but developphp is a very good learning site all in all

  • Author

Thanks for the above comments guys it has given me an insight as to how i should write code in future.


As i said though i am VERY new to PHP, html i have a bit of experience in and using the <font> tag was just to change the color until i had the script working, due to the site being a black background i needed to be able to see the font, this is still in the testing stage so i still have a lot to learn and code.


Again thanks for the comments and the links to Mr. Khory's site. I hope i can learn something valuble.

Also can anyone recommend how i can process the form so that it checks for usernames and passwords? I don't have the time to code MySql databases as i have messed about with them already and created a folder in my FTP that i now cannot delete, even as administrator. The site needs to go live at the end of the month as it is for work.

 

Thanks and Kind Regards,

Ben.

  • Author

That is some crazy login script! Is that all of the code? I can't see any code to handle the actual login itself?

 

Your telling me! I'm new to coding yet even i know it's wrong! a lot of the code had been from a template where as some i put in myself. I just need help on how to process the login so that the php knows the user is logged in and also so that it knows the password is correct. So far during my playing with the code i have had it so that it stays logged in but didnt have to be correct password and now i have it so that it recognises the password but wont process the form or check users log on.

 

Really stuck on this and any help would really be appreciated, I need to do a course on php really but in the meantime if someone was able to assist with where i am going wrong it would not go unnoticed and i would be willing to reward such a person for helping me to not only code the log on but to possibly explain what some/most of the code actually does.

 

Thanks and Kind Regards,

Ben.

If this is for work, I fear you may need to outsource this. As much as this might sound a little harsh, it's supposed to be for your own benefit; that code is unacceptable for modern day release. It's using outdated approaches to PHP programming, invalid syntax, inline CSS and deprecated HTML.

 

I know that doesn't help your situation much at all, but honestly the whole thing needs re-writing - and for what is essentially a fully blown authenticated website, albeit very small, I doubt anyone is going to have the time to post a solution here for free.

 

I will point you towards a framework that is fairly simple to pick up, however object-oriented programming - in PHP or not - is a reasonably steep learning curve for someone who has never programmed. Try looking at CodeIgniter, and reading about MVC frameworks.

 

As I said, outsourcing is probably your best option right now. With such a tight deadline, it's unlikely you're going to be able to pick up all the necessary skills to create a secure login area, on a cross-browser compatible site. A freelancer, however, could do - very easily.

 

As for MySQL - how have you created a folder with MySQL? It's a database package - not file management. The folder almost certainly has nothing to do with MySQL, or you took a very odd approach to it. Try to CHMOD the folder (and all enclosed items) to 777, then deleting it.

  • Author

I apologise but to me, this is of no use whatsoever. i understand if i had the time to read up on MVC and Codeigniter but as of now i have a video to produce 2 websites to maintain and a logon script to put together so at the moment thanks but no thanks.

 

All i require is just a simple log on script *i dont even care if the password is shown in source* the people who will be visiting the website are just after a place where they can log in, browse the content and straight through click to email their clients, all of that is already coded bar the login.

 

I'm basically looking at something like this:

 

<form name="form1" method="post" action="checkpw(dot)php">

<span id="pass">Password:</span>

<span id="formfield"><input name"pw" type="password" id"pw"></span>

<span class="submitbt"> <input type="submit" name="submitbt"></span>

 

I know this wouldnt show code in source as it has a secondary file named checkpw(dot)php and then another file as a php attachment called protect_page(dot)php which would be linked using:

<?php include('protect_page(dot)php') ?>

 

I spent alot of last night reading up about password protection and i feel im almost there. is the above code considered as a correct format? and my only problem now is that when my site runs the log in script it displays

Error 405 Method Not Allowed

The requested method POST is not allowed for URL /checkpw(dot)php

 

Yet my checkpw(dot)php file looks like this....

<?php
$pw=$_POST['pw'];

$pw = stripslashes($pw);


if($pw == '*******'){
session_register("pw");
header("location:protect_page(dot)php");
} else {
header("location:wrong(dot)php");
}
?>

 

Can you inform me if i am getting somewhat closer?


Thanks and Kind Regards,
Ben.

I apologise but to me, this is of no use whatsoever. i understand if i had the time to read up on MVC and Codeigniter but as of now i have a video to produce 2 websites to maintain and a logon script to put together so at the moment thanks but no thanks.

 

All i require is just a simple log on script *i dont even care if the password is shown in source* the people who will be visiting the website are just after a place where they can log in, browse the content and straight through click to email their clients, all of that is already coded bar the login.

 

I'm basically looking at something like this:

 

<form name="form1" method="post" action="checkpw(dot)php">

<span id="pass">Password:</span>

<span id="formfield"><input name"pw" type="password" id"pw"></span>

<span class="submitbt"> <input type="submit" name="submitbt"></span>

 

I know this wouldnt show code in source as it has a secondary file named checkpw(dot)php and then another file as a php attachment called protect_page(dot)php which would be linked using:

<?php include('protect_page(dot)php') ?>

 

I spent alot of last night reading up about password protection and i feel im almost there. is the above code considered as a correct format? and my only problem now is that when my site runs the log in script it displays

Error 405 Method Not Allowed

The requested method POST is not allowed for URL /checkpw(dot)php

 

Yet my checkpw(dot)php file looks like this....

<?php

$pw=$_POST['pw'];

 

$pw = stripslashes($pw);

 

 

if($pw == '*******'){

session_register("pw");

header("location:protect_page(dot)php");

} else {

header("location:wrong(dot)php");

}

?>

 

Can you inform me if i am getting somewhat closer?

 

Thanks and Kind Regards,

Ben.

 

Id take Andys advice u don't even need to use MVC or a framework but u atleast are going to either need to outsource or spend a few months or so learning HTML,CSS and PHP standards. We are not saying this to be harsh but in all reality no one on this forum is going to probably post a solution to your code for free considering it needs so much reworking. Now im sure your cursing me out about now to yourself lol but none the less we are speaking the truth.

Since you're new to php and need a quick solution, I would suggest using cookies as they're easier to learn.

 

btw session_register is deprecated as of php 5.4 so your script will soon fail anyway.

 

Is the password stored in a database?

Sorry, that's a really poor attitude, you'll get little response thinking like that. If you're not willing to put in the time required to do something properly, don't do it all. The internet already has too many poorly made, insecure websites - don't add to this.

 

YOu may have better luck contacting a freelancer (on this forum or otherwise) to help you with your system. This will obviously incurr some cost, but will be done properly.

I don't think he has much interest in taking advice from us sadly considering he has failed to even reply back on this topic at all

  • 2 weeks later...
  • Author

Apologies i had completely forgot about the forum, i need to set up a way of it emailing me whenever i receive a reply! Thanks for all the advice and i meant no disrespect in saying i had no time, have had a stressfull month, no excuse i know but the way i was spoke to it just rallied me up so again apologies!

 

As for the freelancer it may be of interest, i have burried my head in php and other such lark over the past week, still no closer than i was, even looked at the MVC framework as suggested by webdesigner93.

 

i'm not requesting for anyone to code it for me without any pay but can someone point me to where i am going wrong and what is deprecated and what is not? i'd be willing to pay if any help can be given, bare in mind im paying out of my own pocket to get a script for my employers website!

Not really sure what the question is anymore. If I understand correctly you require a login form which will check the validity of the password, which is not stored in a database. You then want the logged in status to transfer across different pages in the website.

 

To securely store the password you can either save an encrypted version of it in a text file outside the webroot directory or in the php file - just be sure you encrypt it. Use html for the display of the login form with an action to a php script to validate the form data and start the session if a valid password has been entered. Then each page should check the session variable and redirect if not valid.

 

An action isn't required for HTML5. In HTML4 the spec lists the action attributre as required, but most broesers work without it.

I did not know this - presumably the default is the current script? Would you say it is still better practice to include the action for clarity or does it not matter?

  • Author

Cheers gadgetgirl,

 

That helps quite a bit even with not understanding how to code it i know what it is i need to do. you have broke it down more for me which has made it a lot easier to understand. I have been reading up quite heavily infact upon PHP, MySQLi and even some degree of PDO, safe to say i understand what each of these now stand for at least, next task is to learn the code haha.

 

I have been using a webpage known as tizag(dot)com? not sure if anyone has heard of it but it has some very nice tutorials that even at beginner level you can get a sense of understanding. The only thing is, i've read up on PHPSession and PHPcookies (as well as variables,include,require, forms, functions and array's) but it does not go into as much detail as i had anticipated and as of yet i am still no further. Can you recommend a good PHP book to read, because to be honest everything i have read on the web so far either seems to be deprecated or completely wrong. I wish to learn PHP the right way and make my own mark in the WDF as a reputable developer!

 

Oh and @Tex0gen i am unable to reply to your mail so if a moderator or someone of equal power can pass my email on to him/her? i dont wish to break the forum rules by posting my email address here or break them in general.

 

Kind Regards,
Ben.

Cheers gadgetgirl,

 

That helps quite a bit even with not understanding how to code it i know what it is i need to do. you have broke it down more for me which has made it a lot easier to understand. I have been reading up quite heavily infact upon PHP, MySQLi and even some degree of PDO, safe to say i understand what each of these now stand for at least, next task is to learn the code haha.

 

I have been using a webpage known as tizag(dot)com? not sure if anyone has heard of it but it has some very nice tutorials that even at beginner level you can get a sense of understanding. The only thing is, i've read up on PHPSession and PHPcookies (as well as variables,include,require, forms, functions and array's) but it does not go into as much detail as i had anticipated and as of yet i am still no further. Can you recommend a good PHP book to read, because to be honest everything i have read on the web so far either seems to be deprecated or completely wrong. I wish to learn PHP the right way and make my own mark in the WDF as a reputable developer!

 

Oh and @Tex0gen i am unable to reply to your mail so if a moderator or someone of equal power can pass my email on to him/her? i dont wish to break the forum rules by posting my email address here or break them in general.

 

Kind Regards,

Ben.

I can pass your email on to him or her just post what you wanna say in this thread and after I pass it on ill remove the message from this thread
  • Author

Thanks wd93, real helpful of you :)

 

Basically if you can say that it would be brilliant and its just to secure 3 pages with a password and that i am thankful for his kindness. but if he would like a sort of payment or gift or anything i would be willing to provide! hopefully i'll be able to provide another 9 posts today and be able to message him/her back once i am a fully pleged member, either that or i will have gold membership on thursday! if you can pass on my email maybe? not sure if you can see it but its just my name (ben fort) with a dot inbetween and the address is at mail dot com. be very gratefull, please if this is breaking the rules at all let me know and i will remove it asap i do not wish to revoke my membership before it has begun!

 

Thanks and kind Regards,

Ben.

I recommend the book PHP and MySQL For Dynamic Websites by Larry Ullman. If you read the whole book and do the scripts, you will be a confident intermediate php programmer afterwards. It covers everything you are looking for plus a whole lot more, is fairly up to date and teaches good programming and security techniques. There are also a couple of good tutorials on Lynda.com but you will need to be aware that as they are fairly old they do use some deprecated functions.

 

What you require is pretty simple and I would be able to provide the scripts. However, if you've already agreed something with another member of this forum I do not want to step on anyone's toes.

 

One thing I forgot to mention - you need to provide a logout link which will logout the user by clearing the session variables and destroying the session.

Thanks wd93, real helpful of you :)

 

Basically if you can say that it would be brilliant and its just to secure 3 pages with a password and that i am thankful for his kindness. but if he would like a sort of payment or gift or anything i would be willing to provide! hopefully i'll be able to provide another 9 posts today and be able to message him/her back once i am a fully pleged member, either that or i will have gold membership on thursday! if you can pass on my email maybe? not sure if you can see it but its just my name (ben fort) with a dot inbetween and the address is at mail dot com. be very gratefull, please if this is breaking the rules at all let me know and i will remove it asap i do not wish to revoke my membership before it has begun!

 

Thanks and kind Regards,

Ben.

I will pass this on either today or tommorow and nah ur ok I believe your a legit enough member that I can help you out
  • Author

Thanks a lot wd93, extremely grateful! and shall not be forgot :)

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.