| | |
php user registration
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 76
Reputation:
Solved Threads: 3
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:
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:
page2:
mysql:
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:
PHP Syntax (Toggle Plain Text)
Warning: Invalid argument supplied for foreach() in C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page1.php on line 31
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 Syntax (Toggle Plain Text)
<?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>
php Syntax (Toggle Plain Text)
<?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; } //=========================================================================================================== ?>
php Syntax (Toggle Plain Text)
<?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()); ?>
Last edited by Lensva; Jan 12th, 2009 at 8:49 am.
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
in you functions you add string data to $_SESSION['error_list'] instead of actually making $_SESSION['error_list'] an array of multiple errors.
Then when you call foreach it is not finding an array because one does not exist.
Try adding [] after each place where you add error data to the error_list session value. This will create a numerically indexed array of multiple errors. $_SESSION['error_list'][] = $error_data;
Then when you call foreach it is not finding an array because one does not exist.
Try adding [] after each place where you add error data to the error_list session value. This will create a numerically indexed array of multiple errors. $_SESSION['error_list'][] = $error_data;
Last edited by mschroeder; Jan 12th, 2009 at 10:10 am.
•
•
Join Date: Mar 2008
Posts: 76
Reputation:
Solved Threads: 3
mschroeder,
added the [ ] tags on lines 17 and 101, however now when all data is entered, no matter valid or not, the output on page1 is
added the [ ] tags on lines 17 and 101, however now when all data is entered, no matter valid or not, the output on page1 is
PHP Syntax (Toggle Plain Text)
Array
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
I wasn't sure what you were doing on line 101 I assumed you were basically looking to see if there was an error and if so you were redirecting to page1 to handle the errors.
In which case you would want to do something like if (!empty( $_SESSION['error_list'] )){ //redirect code }
if you do a print_r($_SESSION['error_list']); what is the structure that is being returned.
In which case you would want to do something like if (!empty( $_SESSION['error_list'] )){ //redirect code }
if you do a print_r($_SESSION['error_list']); what is the structure that is being returned.
•
•
Join Date: Mar 2008
Posts: 76
Reputation:
Solved Threads: 3
to make it more clear to you and others,
lines 99-104 check if there are errors in functions
non_db_username_check(&$error_list);
password_check(&$error_list);
non_db_email_check(&$error_list);
so that if there are, there wouldnt be connection to mysql, else it conects;
changed it to
like you suggested, however the output on page1 is the same
EDIT: ok the output in fact changed, but not for the better, more errors, all coming from the function mysql_data_check(&$error_list)
EDIT EDIT: sample data filled after changed were made:
username: user1; password:user1; password_verify:user2; email: user1
output on page2
lines 99-104 check if there are errors in functions
non_db_username_check(&$error_list);
password_check(&$error_list);
non_db_email_check(&$error_list);
so that if there are, there wouldnt be connection to mysql, else it conects;
changed it to
PHP Syntax (Toggle Plain Text)
function mysql_data_check(&$error_list) { if( !empty($_SESSION['error_list']) ) { $_SESSION['error_list'][] = &$error_list; header('Location:page1.php'); return false; }
EDIT: ok the output in fact changed, but not for the better, more errors, all coming from the function mysql_data_check(&$error_list)
EDIT EDIT: sample data filled after changed were made:
username: user1; password:user1; password_verify:user2; email: user1
output on page2
PHP Syntax (Toggle Plain Text)
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page2.php on line 115 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page2.php on line 118 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page2.php on line 121 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page2.php on line 121 Warning: array_push() [function.array-push]: First argument should be an array in C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page2.php on line 123 Warning: Cannot modify header information - headers already sent by (output started at C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page2.php:115) in C:\Users\admin\Desktop\xampp\xampp\htdocs\examples\user registration\page2.php on line 18
Last edited by Lensva; Jan 12th, 2009 at 11:26 am.
•
•
Join Date: Mar 2008
Posts: 76
Reputation:
Solved Threads: 3
===major update===
after fixing a few bugs the situation seems for the much better;
updated version:
page2 seems to be working, however on page1 $_SESSION['error_list'] displays ('Array' number varies) instead of the actual messages. unless the form is submited empty, then everything works
after fixing a few bugs the situation seems for the much better;
updated version:
php Syntax (Toggle Plain Text)
<?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_list; 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; } //=========================================================================================================== 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( !empty($_SESSION['error_list']) ) { $_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 FROM user_info WHERE username=\"$safe_username\"") or die(mysql_error()); $query_email = mysql_query("SELECT email FROM user_info WHERE email=\"$safe_email\"") or die(mysql_error()); //---------------------------------------- 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_list, "registration data correct"); return true; } //=========================================================================================================== ?>
PHP Syntax (Toggle Plain Text)
ArrayArray
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
that is because in the mysql_data_check function you are doing this:
$_SESSION['error_list'][] = &$error_list;
which i believe is setting and entry of $_SESSION['error_list'] equal to an array. you don't need the []'s there
Produces:
$_SESSION['error_list'] = array(
1 => array (
1=> 'Error #1';
),
2 => array (
1 => 'Error #1',
2 => 'Error #2'
)
);
The only place you need the []'s is when you do $_SESSION['error_list'][] = $error_data; This way you end up with one array that contains each error message.
Produces:
$_SESSION['error_list'] = array (
1 => 'Error #1',
2 => 'Error #2'
);
$_SESSION['error_list'][] = &$error_list;
which i believe is setting and entry of $_SESSION['error_list'] equal to an array. you don't need the []'s there
Produces:
$_SESSION['error_list'] = array(
1 => array (
1=> 'Error #1';
),
2 => array (
1 => 'Error #1',
2 => 'Error #2'
)
);
The only place you need the []'s is when you do $_SESSION['error_list'][] = $error_data; This way you end up with one array that contains each error message.
Produces:
$_SESSION['error_list'] = array (
1 => 'Error #1',
2 => 'Error #2'
);
•
•
Join Date: Mar 2008
Posts: 76
Reputation:
Solved Threads: 3
did as you adviced changed
and
seems it helped for the most part, but when i enter all data incorectly it displays:
one last piece of error somewhere i think
php Syntax (Toggle Plain Text)
function mysql_data_check(&$error_list) { if( !empty($_SESSION['error_list']) ) { $_SESSION['error_list'] = &$error_list; header('Location:page1.php'); return false; }
php Syntax (Toggle Plain Text)
$_SESSION['error_list'][] = $error_list;
PHP Syntax (Toggle Plain Text)
<ul><li>invalid characters in username</li> <li> passwords dont match</li> <li> email is invalid</li> </ul>Array
Last edited by Lensva; Jan 12th, 2009 at 4:52 pm.
![]() |
Similar Threads
- User Registration (Site Layout and Usability)
- Php logout (PHP)
- php.ini confusion (PHP)
- having a weird problem on registration page (PHP)
- stuck with user registration (HTML and CSS)
- function in php (PHP)
- Parse error please help (PHP)
- question about connecting odbc to sql through php script (PHP)
- php wont submit data into the database (PHP)
Other Threads in the PHP Forum
- Previous Thread: how to install and test zend framework 1.7.2
- Next Thread: php login
| Thread Tools | Search this Thread |
ajax apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion regex remote script search server session sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validator variable video virus votedown web window.onbeforeunload=closeme; xml youtube





