Forum Moderators: coopster

Message Too Old, No Replies

Few problems in PHP

mainly vars and if statements..

         

firemaster

4:17 am on Jun 17, 2003 (gmt 0)

10+ Year Member



okay, I have been writing my user registration script for a day or so now. I am having the problems that
1.

 
$nameamt = strlen($name);
//check to make sure user did not enter less the 4 or more then 16 characters
if ($nameamt <4 )
{
echo "<b>error:</b> Username must be no less then 4 characters and no less then 16, please try again.";
exit;

This doesn't work, doesn't do anything what so ever..

2.

 //check to make sure passwords and email address match
if($pass!= $pass2)
{
echo "<b>error:</b> Passwords do not match, please re-enter the passwords.";
exit;
}

s doesn't work either.. have no clue why not.

3. I am using a 'homemade' function to make sure the username contains only alphbetic and numeric characters, it like the rest doesn't work, and I am not sure if I am doing this right?

function valid_entry($entry)
{
// check an entry to only be numbers and letters
if (preg_match("[^a-zA-Z0-9]+", $entry))
return true;
else
return false;
}

I appricate any help that is offered.. thanks a lot! Sorry about the bold, just trying to distinguish..
-FM-

Paul in South Africa

7:58 am on Jun 17, 2003 (gmt 0)

10+ Year Member



This is a simplified version of a login script that I have used with some success. There are probably better ways of doing it, but this has worked for me.

$message = "";
if (isset ($flag) && $flag=="join")
{
if (empty ($login) ¦¦ empty ($password) ¦¦ empty ($password2))
$message .="You must fill in all the fields<br>\n";
if ($password!= $password2)
$message .="The passwords you entered did not match<br>\n";
if (strlen ($login) < 6)
$message .="Your login name must be at least 6 characters long<br>\n";
if (strlen ($login) > 10)
$message .="Your login name must not be more than 10 characters long<br>\n";
if (strlen ($password) < 6)
$message .="Your password must be at least 6 characters long<br>\n";
if (strlen ($password) > 10)
$message .="Your password must not be more than 10 characters long<br>\n";
if (//function that checks to see if login has already been used)
$message .="The login name \"$login\" already exists. Please choose another login name<br>\n";
if ($message=="") // no errors found
{
//Process form and got to next page
}
}

If $message is not an empty string then the original page is displayed again with the addition of the message at the top.

I can't help with the regular expression as I'm still trying to get my head round that.

Remeber to replace any ¦ characters with a solid pipe character as the forum software changes it.

Robber

8:35 am on Jun 17, 2003 (gmt 0)

10+ Year Member



Have you checked to make sure the variables you are testing have actually got data in them (eg $name, $pass, $pass2)?

jatar_k

3:47 pm on Jun 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You also may need to access them via the $_POST super global array like so

$nameamt = strlen($_POST['name']);

if($_POST['pass']!= $_POST['pass2'])

PHP Predefined Variables [php.net]

torrent

7:03 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



As for the regexp:

preg_match('/^[a-zA-Z0-9]+$/', $entry)

firemaster

9:18 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



thanks for the help..

I tried $HTTP_POST_VARS['var'] and $_POST['var'] and niether worked, and yes globals are on. I have checked the vars to make suer that they have data in them and that the strlen() do result as intergers..

As for the preg_match, it did not work niether.. I also tried "return(true)"; & "return(false)"; with no luck ..this is very disapointing to me..
Maybe something somewhere else is the code is the problem?
Here is my full code for the pages:

<snip>

Thanks again everyone for trying to help, I hope we can get this to work.
-FM-

[edited by: jatar_k at 9:26 pm (utc) on June 17, 2003]
[edit reason] sorry no urls, see TOS [/edit]

jatar_k

10:09 pm on Jun 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



here is what I see, php 4.3.1, register_globals on

The form

<form name="user_reg" method="post" action="register.php">
<input type="text" name="name" maxlength="16">
<input type="password" name="pass" maxlength="16">
<input type="password" name="pass2" maxlength="16">
<input type="text" name="email">
<input type="text" name="email2">
<input type="submit" name="register" value="Register">

no probs there

Things I would suggest

1. If you re include the form when there is an error, be kind and reload the entered data into the fields.

2. Because you have php 4.3.1 use $_POST not $HTTP_POST_VARS

3. start simple, you have functions and all sorts of stuff, makes it difficult. Start with baseline testing and then flush it out as things work. It will stop you from burying errors and keep trouble to a min.

What would I do? scrap what you have for the moment and try a few basic tests.

I always start with something like this

$errormess = "";
if (empty($_POST['name'])) $errormess .= "<p>name empty";
if (empty($_POST['pass'])) $errormess .= "<p>pass empty";
if (empty($_POST['pass2'])) $errormess .= "<p>pass2 empty";
if (empty($_POST['email'])) $errormess .= "<p>email empty";
if (empty($_POST['email2'])) $errormess .= "<p>email2 empty";

if ($_POST['pass']!= $_POST['pass2']) {
$errormess .= "<p>passwords dont match";
} else if (strlen($_POST['pass']) < 4) {
$errormess .= "<p>password is too long";
}

if ($_POST['email']!= $_POST['email2']) {
$errormess .= "<p>email doesnt match";
} else if (strlen($_POST['email']) < 4) {
$errormess .= "<p>email is too long";
}

if ($errormess!= "") {
echo "<p>errors:<br>",$errormess;
} else {
echo "It works!";
}

very simple but it makes sure I have everything right

firemaster

7:01 pm on Jun 18, 2003 (gmt 0)

10+ Year Member



thanks jatar k, simple is better, code works flawless..
here is what I ended up using, after adding my snipets and what not:

$errormess = "";
if (empty($_POST['name'])) $errormess .= "Username<br>";
if (empty($_POST['pass'])) $errormess .= "Password<br>";
if (empty($_POST['pass2'])) $errormess .= "Repeated Password<br>";
if (empty($_POST['email'])) $errormess .= "E-mail Address<br>";
if (empty($_POST['email2'])) $errormess .= "Repeated E-mail Adress<br>";
if ($errormess!= "")
{
echo "<b>You did not fill out the following:</b><br>",$errormess;
exit;
}
if (!valid_entry($_POST['name'])) {
$errormess .= "Username contains invalid characters<br>";
}
if (strlen($_POST['name']) < 4) {
$errormess .= "Username is too short<br>";
}
if ($_POST['pass']!= $_POST['pass2']) {
$errormess .= "passwords dont match<br>";
} else if (strlen($_POST['pass']) < 4) {
$errormess .= "password is too short<br>";
}
if (!valid_entry($_POST['pass'])) {
$errormess .= "Password contains invalid characters<br>";
}
if ($errormess!= "") {
echo "<b>The following errors occured:</b><br>",$errormess;
exit;
}
if ($_POST['email']!= $_POST['email2']) {
$errormess .= "email doesnt match<br>";
} else if (strlen($_POST['email']) < 4) {
$errormess .= "email is too short<br>";
} else if (!valid_email($_POST['email'])) {
$errormess .= "email is not a correct format<br>";
}
if ($errormess!= "") {
echo "<b>The following errors occured:</b><br>",$errormess;
exit;
}

thanks again all,
-FM-

jatar_k

7:05 pm on Jun 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are you reloading the entered data into the fields now as well?

firemaster

5:23 am on Jun 19, 2003 (gmt 0)

10+ Year Member



I am not sure if I know what you mean Jatar, I added more then what I posted before. The scripts run great, does everything I want it to do. (checks length, valid characters, all filled, if passwords and emails are the same, if username and password are the same, and if the username is already in the DB.)

I do have another question, I guess it would be fine to ask on this same thread.

I don't have a large knowledge with MySQL or the PHP commands for it, how would I go about displaying the data I have pulled from the db? or changing the pulled data into a var?

ex: "SELECT email FROM members WHERE name = '$valid_user'"
how would I display the user's email address? I registered the username as $valid_user, would I go about doing it the same way? registering as vars?
thanks again,
-FM-

jatar_k

4:24 pm on Jun 19, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



When I tried your form (yes I followed the urls before I snipped them ;)) if I got an error it would tell me the error and reload the form. Great but all the values I had enetered were not there so i had to re-enter everything.

kind of a pain

PHP MySQL Functions [ca.php.net]
(I won't link every function below since they are all on the above page)

Using mysql and php is actually quite simple. The two main things that need to be done every time are
connect = mysql_connect
select database = mysql_select_db

Then you have a link to your mysql db and can fire queries at will.

I always do queries in 2 steps helps me be able to debug by echo'ing my query if need be.

1. build query into a string
$q = "SELECT email FROM members WHERE name = '$valid_user'"

2. call mysql_query
$query = mysql_query($q) or die (mysql_errno().": ".mysql_error()."<BR>");

This will return a resource identifier if successful or die and display the error num and error message from mysql, another helpful debugging tool.

there are a couple of functions to get the actual data from the resource returned from the mysql_query call. For the most part I use mysql_fetch_array. It returns an array of that row and is accessible by numeric or named indexes so it is the most versatile. Normally you can run through the returned rows using a loop.

while($row = mysql_fetch_array($query)) {
}

This loop executes as long until there are no more rows. Column values can be accessed two ways.
$row['columnname'] or $row[0] (where 0 is the numeric index for the column)

if you were returning one field you may use this
$memberemail = mysql_fetch_field($query)

if you wanted to count the number of rows returned you could do
$numrows = mysql_num_rows($query);

You can read through the short descriptions beside each function in the top link and it will help you understand what else you can do.

Make more sense?

firemaster

8:08 pm on Jun 19, 2003 (gmt 0)

10+ Year Member



that makes a lot more sense, thanks for that and the link.

The fields still need to be re-entered as they are cleared when you return to the page, I made the errors apear on a second page instead of the same page, so that the errors at the bottom aren't displayed when it is loaded for the first time. I do not know how to have the fields stay filled in, I am sure I could figure something out, but I would with out a doubt go the long way about it.

Thanks,
-FM-