I'm a noob with php. I want to create a simple login page with php so that if you enter a nick that isn't in the database it will tell you such, otherwise it will tell you password is incorrect. I'm using my school server so certain questions about the server I may not be able to answer. Most importantly, I don't have permission to use fopen() so file_put_contents it is. I don't even know if that will work either because I haven't got that to work. So far I have login.php, check.php and numberconverter.php which is a function that helps me convert a number into a string. (In this case, the unix time). I know numbercoverter.php works from testing.
login.php:
<?
define('__ROOT__',dirname(dirname(__FILE__)));
require_once(__ROOT__.'/shopsite/numberconverter.php');
echo "<!--root: ".__ROOT__."/shopsite/ -->\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login to the Shopsite</title>
<script type="text/css">
"error" {
background-color:#FF;
color:#FFFFFF;
}
</script>
<script type="text/javascript">
<?php
$nick="";
$i=false;
if(isset($_GLOBALS['file']))
{
$i=true;
$nick=file_get_contents("file.txt");
$nick=preg_split("^nick:",$nick);
}
else
{
$_GLOBALS['file']='file'.convert(time()).'.txt';
}
?>
functions error(i)
{
if(i)
{
document.getElementById('error').innerHTML="Nick <?php echo $nick; ?> was not found. Try again or <a href=\"register.php\">register</a>";
}
else
{
document.getElementById('error').innerHTML="Password incorrect. Try again or <a href=\"register.php\">register</a>";
}
}
//-->
</script>
</head>
<body <?php if($i) { echo "onload=\"error(".((strlen($nick[0])<0) ? 1 : 0).")\""; }?> >
<p id="error"></p>
<?php
$chk = time();
echo "File name: ".$_GLOBALS['file']."\n";
echo "unix time raw: ".$chk."\n";
echo "unix time converted: ".convert($chk)."\n";
?>
<form id="form" action="check.php" method="post">
<p>username: <input type="text" name="nick" id="nick" /></p>
<p>password: <input type="text" name="pass" id="pass" /></p>
<input type="submit" value="Log in"/>
</form>
</body>
</html>
check.php:
<?php
$c = mysql_pconnect("localhost","hehe","hehe");
mysql_select_db("test",$c);
$r = mysql_query(sprintf("select * from UserTable where (nick=(\"%s\") AND pass=SHA1(\"%s\"))",$_POST['nick'],$_POST['pass']),$c) or die("something wrong with mysql,1");
if(mysql_num_rows($r)==0)
{
$testn = mysql_query(sprintf("select * from UserTable where nick=(\"%s\")",$_POST['nick']),$c) or die("something wrong with mysql,2");
if(mysql_num_rows($testn)==0)
{
//file_put_contents($_GLOBALS['file'],"nick:".$_POST['nick']);//
file_put_contents('test.txt',"nick");
}
else
{
//file_put_contents($_GLOBALS['file'],"pass");
file_put_contents('test.txt',"pass");
}
mysql_close($c);
header("Location:http://cs4.sunyocc.edu/~j.d.dancks/shopsite/login.php");
}
else
{
mysql_close($c);
session_start();
$_SESSION['nick'] = $_POST['nick'];
$_SESSION['email'] = $_POST['email'];
}
?>
Ok. I would like to get the ball rolling again, and get people's input. I can't do mysqli apparently because even though its 4.3.2 I guess its not installed or something. phpinfo: cs4.sunyocc.edu/~j.d.dancks/info.php Anyway, here is what I tried:
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login to the Shopsite</title>
<script type="text/css">
"error" {
background-color:#FF;
color:#FFFFFF;
}
"attempts" {
color:#FF;
}
</script>
<script type="text/javascript">
function errorfunc()
{
<?php
if(isset($_SESSION['msg']))
{
echo "document.getElementById(\'error\').innerHTML=".$_SESSION['msg'];
}
if(isset($_SESSION['attempts']))
{
echo "document.getElementById(\'attempts\').innerHTML=".$_SESSION['attempts']."Of 5 login attempts used.";
}
?>
}
//-->
</script>
</head>
<body onload="errorfunc()">
<?php
$ok = true;
if(isset($_SESSION['attempts']))
{
if($_SESSION['attempts']>=5)
{
echo "<h1>YOU HAVE MAXED OUT YOUR LOGIN ATTEMPTS. COME BACK ANOTHER DAY</h1>";
$ok = false;
}
}
if($ok)
{
echo "<p id=\"error\"></p>\n
<p id=\"attempts\"></p>\n
<form id=\"form\" action=\"check.php\" method=\"post\">\n
<p>username: <input type=\"text\" name=\"nick\" id=\"nick\" /></p>\n
<p>password: <input type=\"text\" name=\"pass\" id=\"pass\" /></p>\n
<input type=\"submit\" value=\"Log in\"/>\n
</form>\n";
}
?>
</body>
</html>
and check.php:
<?php
session_start();
$c = new mysqli("localhost","jddancks","zomglol","test");
if($c->connect_errno())
{
echo "Something is wrong with the mysql connection. To DREAMWEAVER!";
}
else
{
$r = $c->query(sprintf($c,"select * from UserTable where (nick=(\"%s\") AND pass=SHA1(\"%s\"))",$_POST['nick'],$_POST['pass']));
if($r->num_rows==0)
{
$testn = $c->query(sprintf($c,"select * from UserTable where nick=(\"%s\")",$_POST['nick']),$c);
if($testn->num_rows()==0)
{
$_SESSION['msg'] = "Nick ".$_POST['nick']."was not found. Check spelling or <a href=\"register.php\">register</a>";
}
else
{
$_SESSION['msg'] = "Password incorrect";
}
$r->close();
$testn->close();
if(!isset($_SESSION['attempts']))
{
$_SESSION['attempts'] = $_SESSION['attempts'] + 1;
}
else
{
$_SESSION['attempts'] = 1;
}
$c->close();
header("Location:http://cs4.sunyocc.edu/~j.d.dancks/shopsite/login.php");
}
else
{
$c->close();
session_start();
$_SESSION['nick'] = $_POST['nick'];
$_SESSION['email'] = $_POST['email'];
header("Location:http://cs4.sunyocc.edu/~j.d.dancks/shopsite/success.html");
}
}
?>
My only concern is that all someone has to do is delete the site cookie in order to try continue brute forcing the password, so maybe I should record # login attempts to a sql table.