Trying to write a login script, but why isn't it working?

Discussion in 'Programming' started by bigturnip, Dec 7, 2008.

  1. #1
    This is my first attempt at writing a login script, I am have problems with the registration page.

    Everything seems to work fine except nothing is entered in the database. The connection to the database seems to work as it checks whether the username or email are already registered. There are no PHP errors generated, just nothing going into the table.

    I've probably missed something obvious, but if been looking at it for ages now and can't work out what.

    Would be really good if someone could take a look and see where I'm going wrong.

    
    <?php 
    
    $do=$_GET['do'];
    if($do == 'register')
    {
    	// Connect to database
    	include $idir.'/db/pmdbcon.php';
    	
    	function usernametaken($username)
    	{
    		global $conn;
    		if(!get_magic_quotes_gpc())
    		{
          		$username = addslashes($username);
       		}
      		$q = "select user from users where user = '$username'";
       		$result = mysql_query($q,$conn);
       		return (mysql_numrows($result) > 0);
    	}
    	
    	function emailtaken($email)
    	{
    		global $conn;
    		if(!get_magic_quotes_gpc())
    		{
          		$email = addslashes($email);
       		}
      		$q = "select email from users where email = '$email'";
       		$result = mysql_query($q,$conn);
       		return (mysql_numrows($result) > 0);
    	}
    	
    	function textmatch($text1, $text2)
    	{
    		if(!get_magic_quotes_gpc())
    		{
          		$text1 = addslashes($text1);
          		$text2 = addslashes($text2);
       		}
       		return ($text1 != $text2);
    	}
    	
    	function datechk($month, $day, $year)
    	{
    		return (checkdate($month, $day, $year));
    	}
    	
    	function minlen ($txtstr, $len)
    	{
    		if(!get_magic_quotes_gpc())
    		{
          		$textstr = addslashes($txtstr);
       		}
       		return (strlen($txtstr) < $len);
    	}
    	
    	function adduser ($username, $password, $email, $dob)
    	{
    		global $conn;
    		$salt="**********";
          		$password = $password.$salt;
    		if(!get_magic_quotes_gpc())
    		{
          			$username = addslashes($username);
          			$password = addslashes($password);
          			$email = addslashes($email);
       		}
       		$actcode = md5($email);
       		$q = "INSERT INTO users (`user`, `pass`, `actcode`, `active`, `join`, `name`, `surname`, `dob`, `email`, `country`) VALUES ('$username', '$password', '$actcode', '0', NOW(), '', '', $dob, $email, '')";
       		mysql_query($q, $conn);
       		return mysql_query($q, $conn);
       	}
    	
       	//Check if username is already in database
       	if(usernametaken($_POST['user']))
    	{
          	$use = $_POST['user'];
          	die("The username: <strong>$use</strong> is already taken, please pick another one.");
       	}
       	
       	//Check if email is already in database
       	if(emailtaken($_POST['email']))
    	{
          	$use = $_POST['email'];
          	die("The email address: <strong>$use</strong> has already been registered, please use another email address.");
       	}
       	
       	//Check if passwords match
       	if(textmatch($_POST['pass'], $_POST['passc']))
    	{
          	die("The two passwords you entered do not match.");
       	}
       	
       	//Check if emails match
       	if(textmatch($_POST['email'], $_POST['emailc']))
    	{
          	die("The two email addresses you entered do not match.");
       	}
       	
       	//Check if dob is valid
       	if(datechk($_POST['dobm'], $_POST['dobd'], $_POST['doby']))
    	{
       	}
    	else
       	{
          	die("The date you entered for your date of birth is not a valid date.");
       	}
    	
    	//Check if username is at least 4 characters
    	if(minlen($_POST['user'], 4))
    	{
    		die ("Username must be 4 or more characters");
    	}
    	
    	//Check if password is at least 6 characters
    	if(minlen($_POST['pass'], 6))
    	{
    		die ("Password must be 6 or more characters");
    	}
    	
    	// Add the new account to the database
     	$md5pass = md5($_POST['pass']);
    	$dobc = $_POST['doby'].'-'.$_POST['dobm'].'-'.$_POST['dobd'];
    	$_SESSION['reguser'] = $_POST['user'];
    	$_SESSION['regresult'] = adduser($_POST['user'], $md5pass, $_POST['email'], $dobc);
    	$_SESSION['registered'] = true;
    	echo "Thank you for registering";
    }
    
    if(!$do){ ?>
    
    <form id="regform" action="register.php?do=register" method="post">
        <table id="regtable" summary="this table is used to enter your details for registration">
        <tr><td><label class="reg" for="user">Username:</label></td><td> <input class="reg" id ="user" name="user" type="text" size="16" tabindex="5001" maxlength="64" accesskey="u" /></td></tr>
        <tr><td><label class="reg" for="pass">Password:</label></td><td> <input class="reg" id="pass" name="pass" type="password" size="16" tabindex="5002" maxlength="64" accesskey="p" /></td></tr>
        <tr><td><label class="reg" for="passc">Confirm Password:</label></td><td> <input class="reg" id="passc" name="passc" type="password" size="16" tabindex="5003" maxlength="64" accesskey="v" /></td></tr>
        <tr><td><label class="reg" for="email">email:</label></td><td> <input class="reg" id="email" name="email" type="text" size="16" tabindex="5004" maxlength="64" accesskey="e" /></td></tr>
        <tr><td><label class="reg" for="emailc">Confirm email:</label></td><td> <input class="reg" id="emailc" name="emailc" type="text" size="16" tabindex="5005" maxlength="64" accesskey="m" /></td></tr>
        <tr><td>Date Of Birth:</td><td>
        <select id="dobd" name="dobd" tabindex="5006">
        <option value="0">-</option>
    <?php
    	for($i=1;$i<32;$i++)
    	{
    		echo "<option value=\"".$i."\">".$i."</option>\n";
    	}
    ?>
        </select>
        <select id="dobm" name="dobm" tabindex="5007">
        <option value="0">-</option>
        <option value="1">January</option>
        <option value="2">February</option>
        <option value="3">March</option>
        <option value="4">April</option>
        <option value="5">May</option>
        <option value="6">June</option>
        <option value="7">July</option>
        <option value="8">August</option>
        <option value="9">September</option>
        <option value="10">October</option>
        <option value="11">November</option>
        <option value="12">December</option>
        </select>
        <select id="doby" name="doby" tabindex="5008">
        <option value="0">-</option>
    <?php
    	for($i=date("Y");$i>1900;$i--)
    	{
    		echo "<option value=\"".$i."\">".$i."</option>\n";
    	}
    ?>
        </select>
        </td></tr>
        <tr><td colspan="2"><input class="reg" id="register" name="register" type="submit" tabindex="5009" value="Register" accesskey="r" /></td></tr>
        </table>
    </form>
    
    <?php
    }
    ?>
    
    PHP:
    EDIT: Sorry just noticed the PHP section, so I have started the thread there: http://forums.digitalpoint.com/showthread.php?t=1141387

    Feel free to delete this one, as it is the wrong place
     
    bigturnip, Dec 7, 2008 IP