PHP Login Script / form

PHP 3 Comments »

Today I have coded a php login script for you guys to have a look at. It is in its most basic form only using variables stored in the script as verification of the users identity but you could adapt it easily for use with a database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
Simple PHP login script
--> By Andrew Pryde
--> http://www.pryde-design.co.uk
*/
 
// EDIT VARS
$username = "1";
$password = "1";
// STOP EDIT
 
// start a session (needed for detecting if the user is logged in or not)
session_start();
 
// function to login the user or tell them that the username // pass is incorrect
function login ($user, $pass)
{
global $username, $password;
if (($user == $username) && ($pass == $password))
{
$_SESSION['LOGGED_IN'] = true;
}
else
{
print "The password and or Username were incorrect";
}
}
 
// function to print the html login form
function print_form ()
{
$form  = '<form action="'.htmlentities($_SERVER['PHP_SELF']).'" method="post">';
$form .= 'Username:<input name="user" type="text" />Password:<input name="pass" type="password" />';
$form .= '<input name="Submit" type="submit" value="Submit" /></form>';
print $form;
}
if ($_SESSION['LOGGED_IN'] == true && empty($_GET['logout']))
{
print 'Members area....<a href="'.htmlentities($_SERVER['PHP_SELF']).'?logout=yes">Logout</a>';
}
elseif (isset($_POST['user']) && isset($_POST['pass']))
{
login ($_POST['user'], $_POST['pass']);
if ($_SESSION['LOGGED_IN'] == true)
{
print 'Members area....<a href="'.htmlentities($_SERVER['PHP_SELF']).'?logout=yes">Logout</a>';
}
}
elseif (($_SESSION['LOGGED_IN'] == true) && ($_GET['logout'] == "yes"))
{
session_destroy();
print 'You have been logged out... <a href="'.htmlentities($_SERVER['PHP_SELF']).'">Login</a>';
}
else
{
print_form();
}
?>

The code should explain its self as I commented it quite vigorously but if not feel free to comment below and or email me with any problems.

Just as a foot note Asome1 will be glad to see that I have used htmlentites() on the PHP_SELF variables in this script if you don’t know what I’m talking about you should read this post .

I have made a newer version of this script called Advanced Mysql / PHP database backed login script I would suggest you have a look at it as it includes some more useful features not seen in this version of the script.

- Andrew

XSS Hole in PHP_SELF

PHP 1 Comment »

It was brought to my attention recently by a reader of the blog that there was a vulnerability in one of my posts (The email sending script). I dismissed it becuase PHP_SELF is a server variable but then he confirmed with a proof of concept.

I was not aware of this and generally my code is very clean and secure so I thought I’d blog about it becuase its something we should all be aware of! XSS is no something to take lightly although it is not as dangerous as remote file inclusion or sql injections there are serious security ramifications from the injection of javascript though xss.

Heres the code in question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
// Enter your email
$to = "andrew@pryde-design.co.uk";
 
// Contact form
$form = "<form name="Email Form" method="post" action="".htmlentities($_SERVER["PHP_SELF"])."">Name<br />";
$form .= "<input type="text" name="name" /><br /><br />Subject<br />";
$form .= "<input type="text" name="subject" /><br /><br />Message<br />";
$form .= "<textarea name="msg" cols="50" rows="5"></textarea><br />";
$form .= "<input type="submit" name="Submit" value="Submit" /></form>";
 
// asks if the form been filled in
if (!empty($_POST["name"]) && !empty($_POST["subject"]) && !empty($_POST["msg"]))
{
//if it has send the data
if (mail($to,$_POST["subject"],$_POST["msg"]))
{
print "Message Sent!";
}
else
{
print "There was an error please contact " . $to . " via your mail cleint";
}
}
else
{
print $form;
}
?>

As you can see after Ausome’s comments I have added htmlentities to the script and there is no longer a problem but let me show you what happened before I added htmlentities.

At the end of the url (/ems.php) I added /”><script>alert(’xss’)</script> the html source ended up like this action=”/ems.php/”><script>alert(’xss’)</script>”> instead of action=”/ems.php”>.

Baslily the moral of this story is if in doubt phrase the variable!

Andrew (thanks to Asome1)

Last Five Wordpress posts on site index

PHP 5 Comments »

As you can see there is a new layout for http://pryde-design.co.uk it includes the latest 5 blog posts as well and I am going to share how I did that with you as it took me ages to work out!

At the top of your page include / require the wordpress ‘header file’ (change the path accordingly). This lets you make use of any WordPress function or template tag in the page.

PHP Code:

<?php require(‘./wordpress/wp-blog-header.php’); ?>

Add this where you want your list of last five posts

PHP Code:

<ul><?php wp_get_archives(‘type=postbypost&limit=5′); ?></ul>

Simple but it was not the way I was trying to do it so it took me ages to find it!

Andrew

PHP Mailing system

PHP 9 Comments »

I have decided to write a tutorial on creating a mailing system In PHP. I do this because I have recently had to build one for a client and I found it quite interesting as I had not used the mail function in ages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?
// Enter your email
$to = "andrew@pryde-design.co.uk";
 
// Contact form
$form = '<form name="Email Form" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">Name<br />';
$form .= '<input type="text" name="name" /><br /><br />Subject<br />';
$form .= '<input type="text" name="subject" /><br /><br />Message<br />';
$form .= '<textarea name="msg" cols="50" rows="5"></textarea><br />';
$form .= '<input type="submit" name="Submit" value="Submit" /></form>';
 
// asks if the form been filled in
if (!empty($_POST['name']) && !empty($_POST['subject']) && !empty($_POST['msg']))
{
//if it has send the data
if (mail($to,$_POST['subject'],$_POST['msg']))
{
print "Message Sent!";
}
else
{
print "There was an error please contact " . $to . " via your mail cleint";
}
}
else
{
print $form;
}
?>

The code should explain all but if not comment and I will get back to you.

Andrew

PHP updater

PHP No Comments »

If you are releasing an application and need it to be able to update its self you can use fopen to open a remote file. for example:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
if($fp = fopen("http://server/file-to-update-from", "r")) {
$content = '';
while($line = fread($fp, 1024)) {
$content .= $line;
}
fclose($fp);
$fh = open("localfile-to-update", "w"); # you can use "a" for append if needed
fwrite($fh, $content);=
fclose($fh);
}
?>

As you can see its very simple to do. You can then create a cron job which will run this update application to check for updates every so often.

Thanks

- Alex

WP Theme & Icons by Pryde Design
Entries RSS Comments RSS Log in