Jump to content

Archived

This topic is now archived and is closed to further replies.

northerncomfort

Having Some Trouble On Register...

Recommended Posts

I'm putting up the frameworks for a register script, but it just won't work! I've fiddled and fiddled, but to no avail. The code's pretty straightforward in what it's doing, which makes me even more confused as to what's wrong!

Any help would be much appreciated!

<?php
include "database.php";
include "header.php";
?>
<div id="main"><div id="content"><div id="text">
<h1>register</h1><p>
<?php
$month = date(m);
$day = date(d);
$year = date(Y);
$name = strip_tags($_POST['name']);
$location = strip_tags($_POST['location']);
$aim = strip_tags($_POST['aim']);
$_POST['email'] = $email;
$_POST['name'] = $name;
$_POST['location'] = $location;
$_POST['username'] = $username;
$username = trim($username);
$_POST['im'] = $aim;
$_POST['pass1'] = $pass1;
$_POST['pass2'] = $pass2;
$_POST['url'] = $url;
$_POST['bias'] = $bias;
$_POST['party'] = $party;
$_POST['list'] = $list;
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "MAAAARRRRRGGGGEEE! Cannot connect to database.");
$q = "SELECT username FROM users WHERE username = '$username'";
$result = mysql_query($q);
$number = mysql_numrows($result);
$if ($number != 0) {
echo "sorry, the username ".$username." is already taken. <a href=\"register.php\">try again</a>.</p>";
exit;
}
if (strlen($username) > 16) {
  echo 'Sorry, ".$username." is longer than 16 characters, please  <a href=\"register.php\">shorten it</a>.';
  exit;
}
if ($pass1 != $pass2) {
echo 'your passwords do not match,  please <a href=\"register.php\">double check them</a>.';
exit;
}
if (strlen($pass1) < 4){
echo 'your password is too short,  please <a href=\"register.php\">lengthen it</a>.';
exit;
}
if (!preg_match("/.*@.*..*/", $email) | preg_match("/(<|>)/", $email)) {
die('your email address appears to be invalid, please <a href=\"register.php\">double check it</a>.');
} else {
$passhash = md5($pass1);
$q = "INSERT INTO users VALUES ('', '$month', '$day', '$year', '$username', '$email', '$passhash', '$name', '$location', '$aim', '$bias', '$party', '$age', '$list', '0')";
result = mysql_query($q);
?>
<div id="loginscreen">
<p>registration was a success! log in now.</p>
<form name="register" action="logmein.php" method="post">
<table border="0" cellspacing="5">
<tr>
<td width="100" align="right"> username:</td>
<td width="10"></td>
<td><input type="text" name="username"></td>
</tr><tr>
<td width="100" align="right"> password:</td>
<td width="10"></td>
<td><input type="text" name="password"></td>
</tr>
<tr><td width="100" align="right">remember me?</td><td width="10"></td><td>
<select name="remember"><option value="yes">yes, please!</option><option value="no">no thank you</option></select>
</td></tr>
<tr>
<td width="100"></td><td width=10"></td><td width="150" align="right"><input type="submit" name="submit" value="log in"</td>
</tr>
</table></form>
</div>
</div></div></div>
<?php
include "sidebar.php";
include "footer.php";
}
?>

Share this post


Link to post
Share on other sites
You'll need to be a bit more detailed in your description. what exactly does [i]doesn't work[/i] mean? Are you getting any errors... what?

Share this post


Link to post
Share on other sites
is this supposed to be the registration and the login page?  Regardless, you have no breakout to perform your PHP functions when your form is submitted all of that information is just processed every time the page is loaded.  All of your error checking code for the $_POST superglobal should ONLY happen if your form has been submitted (ie:

[code]if ($_POST['submit']) {
// start checking submitted values
} [/code]

aside from the includes, the only code your PHP interpreter should be parsing prior to the user submitting information is the HTML code for the form... all of the other PHP error trapping needs to occur on a condition like described in the above code.

Share this post


Link to post
Share on other sites
Also you are assigning $var to $_POST['varname'].  It should be the other way around.

[code]
$month = date(m);
$day = date(d);
$year = date(Y);
$name = strip_tags($_POST['name']);
$location = strip_tags($_POST['location']);
$aim = strip_tags($_POST['aim']);
$email = $_POST['email'];
$name = $_POST['name'];
$location = $_POST['location'];
$username = trim($_POST['username']);
$aim = $_POST['im'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$url = $_POST['url'];
$bias = $_POST['bias'];
$party = $_POST['party'];
$list = $_POST['list'];
[/code]

Share this post


Link to post
Share on other sites
I fixed those two things- the detect submit was something I was going to add in a bit later, but it can't hurt to put it in now. I also swapped the $var and the $_POST['var'], that one always gets me for some reason! I also fixed a few typos (a $ in front of an if, sheesh)

Still no luck, but perhaps there's another glaringly obvious thing I'm goofing up?

[code]
<?php
include "database.php";
include "header.php";
?>
<div id="main"><div id="content"><div id="text">
<h1>register</h1><p>
<?php
if (isset($$_POST['submit'])) {
$month = date(m);
$day = date(d);
$year = date(Y);
$name = strip_tags($_POST['name']);
$location = strip_tags($_POST['location']);
$aim = strip_tags($_POST['aim']);
$email = $_POST['email'];
$name = $_POST['name'];
$location = $_POST['location'];
$username = $_POST['username'];
$username = trim($username);
$aim = $_POST['im'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$url = $_POST['url'];
$bias = $_POST['bias'];
$party = $_POST['party'];
$list = $_POST['list'];
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "MAAAARRRRRGGGGEEE! Cannot connect to database.");
$q = "SELECT username FROM users WHERE username = '$username'";
$result = mysql_query($q);
$number = mysql_numrows($result);
if ($number != 0) {
echo "sorry, the username ".$username." is already taken. <a href=\"register.php\">try again</a>.</p>";
exit;
}
if (strlen($username) > 16) {
  echo 'Sorry, ".$username." is longer than 16 characters, please  <a href=\"register.php\">shorten it</a>.';
  exit;
}
if ($pass1 != $pass2) {
echo 'your passwords do not match,  please <a href=\"register.php\">double check them</a>.';
exit;
}
if (strlen($pass1) < 4){
echo 'your password is too short,  please <a href=\"register.php\">lengthen it</a>.';
exit;
}
if (!preg_match("/.*@.*..*/", $email) | preg_match("/(<|>)/", $email)) {
die('your email address appears to be invalid, please <a href=\"register.php\">double check it</a>.');
}
else {
$passhash = md5($pass1);
$q = "INSERT INTO users VALUES ('', '$month', '$day', '$year', '$username', '$email', '$passhash', '$name', '$location', '$aim', '$bias', '$party', '$age', '$list', '0')";
result = mysql_query($q);
?>
<div id="loginscreen">
<p>registration was a success! log in now.</p>
<form name="register" action="logmein.php" method="post">
<table border="0" cellspacing="5">
<tr>
<td width="100" align="right"> username:</td>
<td width="10"></td>
<td><input type="text" name="username"></td>
</tr><tr>
<td width="100" align="right"> password:</td>
<td width="10"></td>
<td><input type="text" name="password"></td>
</tr>
<tr><td width="100" align="right">remember me?</td><td width="10"></td><td>
<select name="remember"><option value="yes">yes, please!</option><option value="no">no thank you</option></select>
</td></tr>
<tr>
<td width="100"></td><td width=10"></td><td width="150" align="right"><input type="submit" name="submit" value="log in"</td>
</tr>
</table></form>
</div>
</div></div></div>
<?php
}
include "sidebar.php";
include "footer.php";
}
else {
exit;
}
?>
[/code]

Share this post


Link to post
Share on other sites
Change this line [code=php:0]if (!preg_match("/.*@.*..*/", $email) | preg_match("/(<|>)/", $email))[/code] to [code=php:0]if (!preg_match("/.*@.*..*/", $email) || preg_match("/(<|>)/", $email))[/code]

Share this post


Link to post
Share on other sites
*sigh.... you are missing the point and this MUST be just a fragment of your registration page??? post the code for the whole page so we can see if your problem lies somewhere outside of where YOU think it does.  Do you even know if your registration form is posting variables the way it should be?  use the following code to look at the $_POST array once your data has been submitted so you can see even if the submitted data is getting transferred after the button's been clicked:

[code]echo "<pre>";
print_r($_POST);
echo "</pre>";[/code]

Share this post


Link to post
Share on other sites
oh... and change if (isset($$_POST['submit'])) { to if (isset($_POST['submit'])) {

and what PHP editor are you using??? a good PHP editor will help you see a lot of coding errors like an unclosed bracket, extra quotations, unescaped quotes, etc...  one i use and swear by is totally free and a sourceforge project.  notepad++ (http://notepad-plus.sourceforge.net/uk/site.htm)

Share this post


Link to post
Share on other sites
Also don't surpass errors for [tt]@mysql_select_db($database) or die( "MAAAARRRRRGGGGEEE! Cannot connect to database.");[/tt].  Remove the @ to see if it gives you any error messages.

Share this post


Link to post
Share on other sites
Here is the complete code. It's changed a bit in the past few hours, but the idea remains the same. But the changes in organization helped me a bit, as now at least I'm not getting a white screen. And the $_POST array is coming through just fine, that's a handy bit of code though, thanks! There aren't any errors when I remove the @ in front of the mysql_select_db. And, I use Bluefish, which gives me some colorized syntax help, but it's not quite as handy as my favorite, BBEdit Lite.

The register page is in two parts, register.php (the form) and registerme.php (where the registration goodness comes into play). Here is the inital register form, register.php

[code]<?php
include "header.php";
?>
<div id="main"><div id="content"><div id="text"><div id="register">
<h1>sign up</h1>
<form name="register" action="registerme.php" method="post">
<table border="0" cellspacing="5">
<tr>
<td width="100" align="right"><b>*</b> username:</td>
<td width="10"></td>
<td><input type="text" name="username"><td width="15"></td></td><td><i>your identity; 16 characters max</td></i>
</tr><tr>
<td width="100" align="right"><b>*</b> email:</td>
<td width="10"></td>
<td><input type="text" name="email"><td width="15"></td></td><td><i>must be a valid email address</td></i>
</tr><tr>
<td width="100" align="right"><b>*</b> password:</td>
<td width="10"></td>
<td><input type="password" name="pass1"><td width="15"></td></td><td><i>minimum of four characters</td></i>
</tr><tr>
<td width="100" align="right"><b>*</b> confirm pass:</td>
<td width="10"></td>
<td><input type="password" name="pass2"><td width="15"></td></td><td><i>just making sure...</i></td></i>
</tr><tr><td width="100"></td><td width="10"></td><td></td></tr>
<tr><td width="100"></td><td width="10"></td><td><i>optional info</i></td></tr>
<tr><td width="100"></td><td width="10"></td><td></td></tr>
<tr>
<td width="100" align="right">real name:</td><td width="10"></td><td><input type="text" name="name"></td>
</tr><tr>
<td width="100" align="right">location:</td>
<td width="10"></td>
<td><input type="text" name="location"></td>
</tr><tr>
<td width="100" align="right">aim/msn/icq:</td>
<td width="10"></td>
<td><input type="text" name="im"></td>
</tr><tr>
<td width="100" align="right">homepage url:</td>
<td width="10"></td>
<td><input type="text" name="url"></td>
</tr>
<tr>
<td width="100" align="right">political bias:</td>
<td width="10"></td>
<td><select width="150" name="bias">
<option value="none">neutral</option>
<option value="liberal3">extremely liberal</option>
<option value="liberal2">moderately liberal</option>
<option value="liberal1">slightly liberal</option>
<option value="conservative1">slightly conservative</option>
<option value="conservative2">moderately conservative</option>
<option value="conservative3">extremely conservative</option>
</select></td>
</tr>
<tr>
<td width="100" align="right">political party:</td>
<td width="10"></td>
<td><select width="150" name="party">
<option value="none">no party for me</option>
<option value="communist">communist</option>
<option value="democrat">democrat</option>
<option value="green">green</option>
<option value="independent">independent</option>
<option value="libertarian">libertarian</option>
<option value="reform">reform</option>
<option value="republican">republican</option>
<option value="socialist">socialist</option>
</select></td>
</tr>
<tr>
<td width="100" align="right">age:</td>
<td width="10"></td>
<td><select width="150" name="age">
<option value="none">not telling!</option>
<option value="under15">under 15</option>
<option value="1518">15 - 18</option>
<option value="1922">19 - 22</option>
<option value="2327">23 - 27</option>
<option value="2833">28 - 33</option>
<option value="3440">34 - 40</option>
<option value="4150">41 - 50</option>
<option value="over50">over 50</option>
</select></td>
</tr>
<tr>
<td width="100" align="right">mailing list:</td>
<td width="10"></td>
<td><select width="150" name="list">
<option value="no">no emails, please!</option>
<option value="yes">yes, why thank you</option>
</select></td>
</tr>
<tr>
<td width="100"></td><td width=10"></td><td width="150" align="right"><input type="submit" name="submit" value="sign up"</td></tr>
</table></form></div></div></div></div>
<?php
include "sidebar.php";
include "footer.php";
?>[/code]

and the register processing page, registerme.php

[code]<?php
function fixvar() {
$month = date(m);
$day = date(d);
$year = date(Y);
$name = strip_tags($_POST['name']);
$location = strip_tags($_POST['location']);
$aim = strip_tags($_POST['aim']);
$name = $_POST['name'];
$location = $_POST['location'];
$aim = $_POST['im'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$url = $_POST['url'];
$bias = $_POST['bias'];
$party = $_POST['party'];
$list = $_POST['list'];
}
function dbconnect() {
include "database.php";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "MAAAARRRRRGGGGEEE! Cannot connect to database.");
}
function inbusiness() {
$username = $_POST['username'];
if (strlen($username) >= 3) {
continuebusiness();
  }
  if (strlen($username) < 3) {
  echo 'sorry, that username is shorter than 3 characters. please  <a href="register.php">lengthen it</a>.</p>';
  echo "</div></div></div>";
include "sidebar.php";
include "footer.php";
  exit;
  }

function continuebusiness() {
$email = $_POST['email'];
if (strlen($email) < 7) {
echo 'your email appears to be invalid,  please <a href="register.php">double-check it</a>.</p>';
  echo "</div></div></div>";
include "sidebar.php";
include "footer.php";
  exit;
  }
  if (strlen($email) > 8) {
  checkuserlength();
  }
}
function checkusername() {
dbconnect();
$q = "SELECT username FROM users WHERE username = '$username'";
$result = mysql_query($q);
$number = mysql_numrows($result);
if ($number != 0) {
echo "sorry, the username ".$username." is already taken. <a href=\"register.php\">try again</a>.</p>";
echo "</div></div></div>";
include "sidebar.php";
include "footer.php";
exit;
} else {
submituser();
}
}
function checkuserlength() {
if (strlen($username) > 16) {
  echo 'sorry, ".$username." is longer than 16 characters, please  <a href="register.php">shorten it</a>.</p>';
  echo "</div></div></div>";
include "sidebar.php";
include "footer.php";
  exit;
} else {
checkpasswords();
}
}
function checkpasswords() {
if ($pass1 != $pass2) {
echo 'your passwords do not match,  please <a href="register.php">double-check them</a>.</p>';
echo "</div></div></div>";
include "sidebar.php";
include "footer.php";
exit;
} else {
checkpasslength();
}
}
function checkpasslength() {
if (strlen($pass1) <= 4) {
echo 'your password is too short,  please <a href="register.php">lengthen it</a>.</p>';
echo "</div></div></div>";
include "sidebar.php";
include "footer.php";
exit;
} else {
checkemail();
}
}
function checkemail() {
  if (ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) {
        echo 'your email appears to be invalid,  please <a href="register.php">double-check it</a>.</p>';
echo "</div></div></div>";
include "sidebar.php";
include "footer.php";
exit;
    } else {
    checkusername();
    }
}
function submituser() {
$passhash = (md5($pass1));
$x = "INSERT INTO users VALUES ('', '$month', '$day', '$year', '$username', '$email', '$passhash', '$name', '$location', '$aim', '$bias', '$party', '$age', '$list', '0')";
$result = mysql_query($x);
}
include "header.php";
?>
<div id="main"><div id="content">
<h1>register</h1><div id="text"><p>
<?php
if ($_POST['submit']) {
fixvar();
inbusiness();
?>
<div id="loginscreen">
<p>registration was a success! log in now.</p>
<form name="register" action="logmein.php" method="post">
<table border="0" cellspacing="5">
<tr>
<td width="100" align="right"> username:</td>
<td width="10"></td>
<td><input type="text" name="username"></td>
</tr><tr>
<td width="100" align="right"> password:</td>
<td width="10"></td>
<td><input type="text" name="password"></td>
</tr>
<tr><td width="100" align="right">remember me?</td><td width="10"></td><td>
<select name="remember"><option value="yes">yes, please!</option><option value="no">no thank you</option></select>
</td></tr>
<tr>
<td width="100"></td><td width=10"></td><td width="150" align="right"><input type="submit" name="submit" value="log in"</td>
</tr>
</table></form>
</div>
<?php
} else {
echo "<div id=\"text\"><p>something definitely went wrong here. please try <a href=\"register.php\">registering</a> again!</p></div>";
echo "</div></div></div>";
include "sidebar.php";
}
echo "</div></div></div>";
include "sidebar.php";
include "footer.php";
?>[/code]

Share this post


Link to post
Share on other sites
ah yes... your problem lies with scope... variable scope to be more specific.  after you run your vars through the functions you've listed (fixvar(), inbusiness()) try to echo some of them out to see what you get.  more than likely, nothing at all.  The reason for this is that you need to either take the actions in fixvar() out of a function or you need to [url=http://www.php3.de/return] return[/url] your variables so that the rest of your code outside of that function can see the variables after you've manipulated them.  also, when in your code are you calling submituser()???  I see you instantiate the function but you don't ever call it?  is this just because you haven't gotten to that point yet?

Edit:  Your welcome on the code snippet.  I use it regularly to verify my data is coming through for multiple arrays, I've actually turned it into a simple function:
[code]function iterateArray($array) {
echo "<pre>";
print_r($array);
echo "</pre>";
return $array;
}[/code]
and then to use:
[code]iterateArray($_POST);
iterateArray($_SESSION);

etc...[/code]

this is a bad example of return as it's not needed but is used in case my array passed is NOT a global one...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.