hello gents,
i have two pages: page1.php takes user input, page2.php should process it and then return page1 either error or success messages.
there seem to be an error(s) i cant figure out:
when you fill the form with correct data you get:
Warning: Invalid argument supplied for foreach() in C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page1.php on line 31
page2 is made of functions to hopefully increase readability;
me being new to php, will probably make a lot of flawed logical decisions, so tips on what to change would be even better than the solution itself;
page1:
<?php session_start();
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
?>
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
#registration div {
width:150px;
height:30px;
float:left;}
#registration span {
font-size:12px;}
br {
clear:both;}
</style>
</head>
<body>
<form id="registration" action="page2.php" method="post">
<div>Username:</div> <input type="text" maxlength="15" name="username"> <span> 3-15 characters </span> <br>
<div>Password:</div> <input type="password" maxlength="15" name="password"> <span>5-20 characters</span> <br>
<div>Verify password:</div> <input type="password" maxlength="20" name="password_verify"> <br>
<div>E-mail:</div> <input type="text" maxlength="30" name="email"> <span>40 characters max</span> <br>
<input type="reset">
<input type="submit" name="submit" value="Register">
<input type="submit" name="delete_session" value="delete session">
</form>
<br>
<?php
if( !empty($_SESSION['error_list']) )
foreach($_SESSION['error_list'] as $key => $value)
echo $value;
?>
</body>
</html>
page2:
<?php
session_start();
$error_list = array();
//delete session==========================================================================================
if( isset($_POST['delete_session']) )
{
session_destroy();
header('Location:page1.php');
}
//validation start===============================================================================
if( parameter_check(&$error_list) )
{
non_db_username_check(&$error_list);
password_check(&$error_list);
non_db_email_check(&$error_list);
mysql_data_check(&$error_list);
$_SESSION['error_list'] = $error_data;
header('Location:page1.php');
}
else
{
$_SESSION['error_list'] = $error_list;
header('Location:page1.php');
}
//functions================================================================================================
function parameter_check(&$error_list)
{
if( (isset($_POST['submit']) ) &&
(!empty($_POST['username']) ) &&
(!empty($_POST['password']) ) &&
(!empty($_POST['password_verify']) ) &&
(!empty($_POST['email']) ) );
else
{
array_push(&$error_list, "<li>fill in all fields</li>");
return false;
}
return true;
}
//functions================================================================================================
function non_db_username_check(&$error_list)
{
$username = $_POST['username'];
//----------------------------------------
if( (strlen($username) >= 3) && (strlen($username) <= 15));
else
{
array_push(&$error_list, "<li>enter 3 - 15 characters username</li>");
return false;
}
//----------------------------------------
if( !ereg("^[A-Za-z0-9.-_]$",$username))
{
array_push(&$error_list, "<li>invalid characters in username</li>");
return false;
}
//----------------------------------------
return true;
}
//===========================================================================================================
function password_check(&$error_list)
{
$password1 = $_POST['password'];
$password2 = $_POST['password_verify'];
//----------------------------------------
if( strcmp($password1,$password2) != 0)
{
array_push(&$error_list, "<li>passwords dont match</li>");
return false;
}
//----------------------------------------
if( (strlen($password1) >= 5) && (strlen($password1) <= 20));
else
{
array_push(&$error_list, "<li>enter 3 - 20 characters password</li>");
return false;
}
//----------------------------------------
return true;
}
//===========================================================================================================
function non_db_email_check(&$error_list)
{
$email = $_POST['email'];
//----------------------------------------
if( ereg("^[^.] [A-Za-z0-9.-_]{1,20} @ [^@.][A-Za-z0-9.-_]{1,40}$",$email) );
else
{
array_push(&$error_list, "<li>email is invalid</li>");
return false;
}
//----------------------------------------
return true;
}
//===========================================================================================================
function mysql_data_check(&$error_list)
{
if( strlen(&$error_list)>0 )
{
$_SESSION['error_list'] = &$error_list;
header('Location:page1.php');
return false;
}
//----------------------------------------
mysql_connect('localhost','root','root') or die(mysql_error());
mysql_select_db('users') or die(mysql_error());
$safe_username = mysql_real_escape_string($_POST['username']);
$safe_email = mysql_real_escape_string($_POST['email']);
$query_username = mysql_query("SELECT username WHERE username=\"$safe_username\"");
$query_email = mysql_query("SELECT email WHERE email=\"$safe_email\"");
//----------------------------------------
if( mysql_num_rows($query_username) != 0 )
array_push(&$error_list, "<li>username already exists</li>");
if( mysql_num_rows($query_email) !=0 )
array_push(&$error_list, "<li>email already exists</li>");
if( (mysql_num_rows($query_username) || mysql_num_rows($query_email)) !=0 ) return false;
//----------------------------------------
array_push(&$error_data, "registration data correct");
return true;
}
//===========================================================================================================
?>
mysql:
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
mysql_query("CREATE DATABASE users") or die(mysql_error());
mysql_select_db('users') or die(mysql_error());
$user_info = "CREATE TABLE user_info ( user_id INT(5) NOT NULL AUTO_INCREMENT,
username VARCHAR(15) NOT NULL UNIQUE,
password VARCHAR(20) NOT NULL,
mail VARCHAR(40) NOT NULL UNIQUE,
ip VARCHAR(15) NOT NULL,
activated VARCHAR(1) NOT NULL DEFAULT 0,
session_id VARCHAR(32) NOT NULL,
PRIMARY KEY(user_id)
)";
mysql_query($user_info) or die(mysql_error());
?>