Registration code error

Discussion in 'PHP' started by nrodes, Nov 8, 2008.

  1. #1
    im trying to teach myself php. I've put together a simple login script. Im making the registration page now. Theres an error i dont recognize. If you see any other problems please tell me about them to.

    error:
    my code:
    <?php
    
    //get form info
    $firstname=$_POST["firstname"];
    $lastname=$_POST["lastname"];
    $username=$_POST["username"];
    $password=$_POST["password"];
    $confirmppassword=$_POST["confirmpassword"];
    $emailaddress=$_POST["emailaddress"];
    $secretquestion=$_POST["secretquestion"];
    $secretanswer=$_POST["secretanswer"];
    
    //make variables some encrypt
    $cpass = md5($password);
    $cuser = md5($username);
    $csecretanswer = md5($secretanswer);
    $file = "users/" . $user . ".php";
    $data = "<?php $cpass=" . $cpass . "; $cuser=" . $cuser . "; $firstname=" . $firstname . "; $lastname=" . $lastname . "; $secretquestion=" . $secretquestion . "; $csecretanswer=" . $csecretanswer . " ?>
    
    
    //make sure all info is ok
    if ($password!=$confirmpassword)
    	{
    	echo "Your password do not match. <a href="register2.htm">Try Again</a><br>; exit(0);
    	}
    
    if (file_exists($file))
    	{
    	echo "Username is in use. <a href="register2.htm">Try Again</a><br>; exit(0);
    	}
    
    //make users file
    $handle = fopen($file);
    fwrite($handle, $data);
    fclose($handle);
    
    head ( 'location: index.htm' );
    
    ?>
    PHP:
    thanks for any help.
     
    nrodes, Nov 8, 2008 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    Your strings are not closed.

    You should use single quotes to define string containing double quotes:

    echo 'Your password do not match. <a href="register2.htm">Try Again</a><br>'; 
    PHP:
    or escape quotes:

    echo "Your password do not match. <a href=\"register2.htm\">Try Again</a><br>"; 
    PHP:
     
    wmtips, Nov 8, 2008 IP
  3. nrodes

    nrodes Peon

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but that didnt fix the error.
     
    nrodes, Nov 8, 2008 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #4
    Is it really so hard to read the error line number, go to that line and correct the error? Your $data declaration contains unterminated string and missing semicolon too.

    Fixed code:

    <?php
    
    //get form info
    $firstname=$_POST["firstname"];
    $lastname=$_POST["lastname"];
    $username=$_POST["username"];
    $password=$_POST["password"];
    $confirmppassword=$_POST["confirmpassword"];
    $emailaddress=$_POST["emailaddress"];
    $secretquestion=$_POST["secretquestion"];
    $secretanswer=$_POST["secretanswer"];
    
    //make variables some encrypt
    $cpass = md5($password);
    $cuser = md5($username);
    $csecretanswer = md5($secretanswer);
    $file = "users/" . $user . ".php";
    $data = "<?php $cpass=" . $cpass . "; $cuser=" . $cuser . "; $firstname=" . $firstname . "; $lastname=" . $lastname . "; $secretquestion=" . $secretquestion . "; $csecretanswer=" . $csecretanswer . " ?>";
    
    
    //make sure all info is ok
    if ($password!=$confirmpassword)
        {
        echo 'Your password do not match. <a href="register2.htm">Try Again</a><br>'; exit(0);
        }
    
    if (file_exists($file))
        {
        echo 'Username is in use. <a href="register2.htm">Try Again</a><br>'; exit(0);
        }
    
    //make users file
    $handle = fopen($file);
    fwrite($handle, $data);
    fclose($handle);
    
    head ( 'location: index.htm' );
    
    ?>
    PHP:
     
    wmtips, Nov 8, 2008 IP