case '':
header('Location: ' . url . '/index.php?page=about'); //if there is no query string, send the browser to the about page
break;



Just spent all fuckin' weekend setting up my LAMP environment(fuckin' with dayum ProFTP, SSH, MySQL GUI client and /var/www rights) in a vm. I'm ready to dig in this PHP shit. JD, much props!
I'm the type of dude that learns best when I have a project which to apply my knowledge. Anybody have any good ideas for web application projects?
Start with form processing. Forms are pretty much a necessity on any website these days. And BTW skip the email validation snippet I have in this tutorial and use the filter function instead.
if(isset($_POST['register']))
{
clean_reg($_POST);
$submit = $_POST;
if(empty($submit['name']))
{
$errors[] = 'Please enter your name.';
}
if(!empty($submit['name']))
{
if(strlen($submit['name']) > $reg_len){
$errors[] = 'Your username is too long.';
}
}
if(empty($submit['email']))
{
$errors[] = 'Please enter your email address.';
}
if(!empty($submit['email']))
{
if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', $submit['email'])) {
$errors[] = 'Please enter a valid email address';
}
if(strlen($submit['email']) > $reg_len){
$errors[] = 'Your email is too long.';
}
}
if(empty($submit['password']))
{
$errors[] = 'Please enter your password.';
}
if(!empty($submit['password']))
{
if(strlen($submit['password']) > $reg_len){
$errors[] = 'Your password is too long.';
}
}
if(empty($errors))
{
$submit['password'] = $submit['password'] . $salt;
$submit['password'] = hash('sha512', $submit['password']);
$submit['date'] = time();
//This block submits the data in to the database using a combination of PHP and MySQL language...
$query ="INSERT INTO `site_users` (`id`, `name`, `email`, `password`, `date`)
VALUES ('', '{$submit['name']}', '{$submit['email']}', '{$submit['password']}', '{$submit['date']}');";
$result = mysql_query($query) or die(mysql_error());
$_SESSION['register'] = array(); //We'll clear the Session data since we don't need it anymore
header('Location: ' . url . '/index.php?action=home'); //This generates a dynamic page after submission that takes to user to where ever you want them to go. This will be covered in a later tutorial.
}
}
if(isset($_POST['register']))
{
clean_reg($_POST);
$submit = $_POST; //Good security practice to copy the $_POST variable
$array_index = -1;
$keys = array_keys($submit);
foreach($submit as $val){
++$array_index;
$key = $keys[$array_index];
if(empty($val)){
$errors[] = 'Please enter your '.$key;
}
if(strlen($val) > $reg_len){
$errors[] = 'Your '.$key.' is too long';
}
}
if(!empty($submit['email'])){ //If the email field is not empty, let's make sure they have entered a legit email address
if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', $submit['email'])) { //We are using regex again to make sure the email address matches the given pattern
$errors[] = 'Please enter a valid email address';
}
}
if(empty($errors))
{
$submit['password'] = $submit['password'] . $salt;
$submit['password'] = hash('sha512', $submit['password']);
$submit['date'] = time();
//This block submits the data in to the database using a combination of PHP and MySQL language...
$query ="INSERT INTO `site_users` (`id`, `name`, `email`, `password`, `date`)
VALUES ('', '{$submit['name']}', '{$submit['email']}', '{$submit['password']}', '{$submit['date']}');";
$result = mysql_query($query) or die(mysql_error());
$_SESSION['register'] = array(); //We'll clear the Session data since we don't need it anymore
header('Location: ' . url . '/index.php?action=home'); //This generates a dynamic page after submission that takes to user to where ever you want them to go. This will be covered in a later tutorial.
}
echo '<pre>', print_r($submit), '</pre>';
}
I corrected ALL of your coding errors (e.g. mysql_real_escape_string() requires a database connection to work, you didn't create the database before attempting to create the table in your mysql scrtipt) AND I made your script alil more efficient.![]()
if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', $submit['email'])) { //We are using regex again to make sure the email address matches the given pattern
$errors[] = 'Please enter a valid email address';
}
if(!filter_var($submit['email'], FILTER_VALIDATE_EMAIL)){
$errors[] = 'Please enter a valid email address';
}
$array_index = -1;
$keys = array_keys($submit);
foreach($submit as $val){
++$array_index;
$key = $keys[$array_index];
if(empty($val)){
$errors[] = 'Please enter your '.$key;
}
if(strlen($val) > $reg_len){
$errors[] = 'Your '.$key.' is too long';
}
}
foreach($submit as $key => $value)
{
if(isset($key)){ //Make sure key exists
if(!empty($value)){ //Make sure value is not empty
if(strlen($value) > $reg_len){
$errors[] = 'Your '.$key.' is too long';
}
}elseif(empty($value)){
$errors[] = 'Please enter your '. $key;
}
}
}
I thought I included the connection script in this tutorial.
Also, isset & empty are much faster than array_keys, which is why I rarely use array_keys.
Also as stated in my previous post, replace:
PHP:if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', $submit['email'])) { //We are using regex again to make sure the email address matches the given pattern $errors[] = 'Please enter a valid email address'; }
with
PHP:if(!filter_var($submit['email'], FILTER_VALIDATE_EMAIL)){ $errors[] = 'Please enter a valid email address'; }
the eregi function will no longer be supported in PHP 6.
if you are going to go this route...
PHP:$array_index = -1; $keys = array_keys($submit); foreach($submit as $val){ ++$array_index; $key = $keys[$array_index]; if(empty($val)){ $errors[] = 'Please enter your '.$key; } if(strlen($val) > $reg_len){ $errors[] = 'Your '.$key.' is too long'; } }
PHP:foreach($submit as $key => $value) { if(isset($key)){ //Make sure key exists if(!empty($value)){ //Make sure value is not empty if(strlen($value) > $reg_len){ $errors[] = 'Your '.$key.' is too long'; } }elseif(empty($value)){ $errors[] = 'Please enter your '. $key; } } }
that's untested but you don't need to incrementing variable if you are using foreach on one array.
foreach($submit as $key => $value)
You did include the connection script but, it wasn't until the table creation lesson further in the tutorial. The mysql_real_escape_string() function was discussed early in the tutorial.
I didn't see the filter_var() function.
Wow, I have to do some research on:
PHP:foreach($submit as $key => $value)
array
(
[name] => Quaid
[site] => bgol
[email] => quaid@bgol.com
)
foreach($_POST as $key => $value)
{
echo $key, ' ', $value, '<br />';
}
array
(
[0] => Quaid
[1] => bgol
[2] => quaid@bgol.com
)
foreach($_POST as $key => $value)
{
echo $key, ' ', $value, '<br />';
}
for($i = 0; count($_POST) >$i; $i++)
{
echo $_POST[$i], '<br />';
}
for($i = 2; count($_POST) >$i; $i++)
PHP:foreach($submit as $key => $value) { if(isset($key)){ //Make sure key exists if(!empty($value)){ //Make sure value is not empty if(strlen($value) > $reg_len){ $errors[] = 'Your '.$key.' is too long'; } }elseif(empty($value)){ $errors[] = 'Please enter your '. $key; } } }
foreach($submit as $key => $value)
{
if(isset($value)){
if(strlen($value) > $reg_len){
$errors[] = 'Your '.$key.' is too long';
}elseif(!isset($value)){
$errors[] = 'Please enter your '. $key;
}
}
I was doing PHP research and I came across PEAR. A code repository makes sense. ???
Also, what framework do you recommend? http://www.phpframeworks.com/
Also, I noticed your PHP directory structure in your tutorial. I read there are different approaches to this. What's your rationale for your approach?
I was doing PHP research and I came across PEAR. A code repository makes sense. ???
Also, what framework do you recommend? http://www.phpframeworks.com/
Also, I noticed your PHP directory structure in your tutorial. I read there are different approaches to this. What's your rationale for your approach?
one more thing, I'm not going to scour through this thread but I believed I emphasized MVC. Which ever framework/approach you decide to use you should always adhere to MVC.
I was doing PHP research and I came across PEAR. A code repository makes sense. ???
Also, what framework do you recommend? http://www.phpframeworks.com/
Also, I noticed your PHP directory structure in your tutorial. I read there are different approaches to this. What's your rationale for your approach?