This is a code to interface with an affiliate program's API it returns 2 responses..

If it is a successful join and the user name and email are valid it returns an
OK
then its supposed to redirect you to a page telling you to check your email

If the user name is taken it says
Handle 'lookimtrix123' is taken. Please enter another handle.
then its supposed to redirect you to a page telling you to try a different user name

The value's of the forms are variable's the form fields are empty and the form validation automatically puts the first error up above the form how do I fix that as well and I don't think I'm submitting the form correctly but I've tried everything looked thru almost 50 pages of help searched the forum, php.net, w3school etc.. I know the basic's of how coding works because I program in other languages and I have done small php stuff like geo locations, display ip etc but this one's stumped me please help

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
<?php
function reg_horn() {
$s = fsockopen('mysite.com', 80);
fwrite($s, "POST /api_join_basic.php?f_email=$email&f_username=$username&campaign_id=20928&api_username=api88&api_password=password HTTP/1.1\nHost: mysite.com\nConnection: close;\n\n");
while(!feof($s)) {
    $line = fgets($s);
      if(stripos($line, $username) !== false) { //Did this because all im looking for is the usename that tells me its invalid
                header("location:fail.php");
        }
        elseif(stripos($line, "OK") !== false) {
                header ("location:success.php");
        }

   else {
                header ("location:error.php");
        }
                break;
        }

fclose($s);
}
?>
</head>
<input name="f_email" type="text" value="<?php echo $email ?>" maxlength="150" /><br />

<input name="f_username" type="text" value="<?php echo $username ?>" size="15" /><br />

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input name="Submit" type="submit" onclick="reg_horn()"/>
</form>
<?php
$ipi=($_SERVER['REMOTE_ADDR']);
$email=($_POST['email']);
$username=($_POST['username']);
//Is username longer then 3 characters?
if(strlen($username)<3)
   {
    echo 'Your username must be longer than 3 characters.<br />';
   }
//Is there a username?
else if(strlen($username)<1)
  {
    echo 'You must enter a username.<br />';
   }  
//Is the E-Mail Valid? else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo 'E-mail is not valid.<br />'; } //Everything looks valid now register! ?> <body> </body> </html>

    If I understand the code correctly, low coffee at the moment so I may be wrong, but you seem to be calling the PHP function from HTML. I think you may not understand how PHP and HTML interface. I am also taking from the supplied code that you don't fully understand HTML (hint: html code works better inside the body tags 😉 )

    A quick primer.

    HTML is what goes to the browser, it has no idea what PHP is and as such will attempt to render any PHP as HTML. PHP runs on the server and generates HTML code which is then passed to the browser.

    Your code would be better something like

    <?php
    
    $ipi=($_SERVER['REMOTE_ADDR']);
    
    // check to see if this is a submission
    if ( isset($_POST['action']) ) {
    
    // get the submitted values to local variables.
    $email=($_POST['email']);
    $username=($_POST['username']);
    
    // value checking.
    
    // Is username longer then 3 characters?
    if ( strlen($username) < 3 ) {
    	echo 'Your username must be longer than 3 characters.<br />';
    }
    //Is there a username?
    elseif( strlen($username) < 1 ) {
    	echo 'You must enter a username.<br />';
    }
    //Is the E-Mail Valid?
    elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    	echo 'E-mail is not valid.<br />';
    }
    //Everything looks valid now register!
    else {
    	reg_horn();
    }
    }
    
    function reg_horn() {
    
    $s = fsockopen('mysite.com', 80);
    fwrite($s, "POST /api_join_basic.php?f_email=$email&f_username=$username&campaign_id=20928&api_username=api88&api_password=password HTTP/1.1\nHost: mysite.com\nConnection: close;\n\n");
    while(!feof($s)) {
    	$line = fgets($s);
    	if(stripos($line, $username) !== false) {
    		//Did this because all im looking for is the usename that tells me its invalid
    		header("location:fail.php");
    		exit;
        } elseif(stripos($line, "OK") !== false) {
    		header ("location:success.php");
    		exit;
    	} else {
    		header ("location:error.php");
    		exit;
        }
    	break;
    }
    fclose($s);
    }
    
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    		<title>Register</title>
    	</head>
    
    <body>
    
    	<form action="" method="POST" >
    		<input type="hidden" name="action" value="formSubmitted" />
    		<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
    
    		<input name="f_email" type="text" value="<?php echo $email ?>" maxlength="150" /><br />
    		<input name="f_username" type="text" value="<?php echo $username ?>" size="15" /><br />
    		<input name="Submit" type="submit"/>
    	</form>
    
    </body>
    </html>

    Now just so this is a learning experience 🙂

    What I have done is seperate out the php from the html. They run seperately so they can be. Also header commands can fail when there is text on the page before they are called, unless the output is buffered. headers that change the pages location are best followed with an exit command just to make sure that the rest of the script isn't run as well.

    When the user hits the submit button the form is submitted back to the same page. So when it enters the script this time the action post variable is set. So the code in the if statement will be run. Which is what we want to happen. Your code executed every time which is why you saw the error message.

    The above code is not perfect, but it does point in the correct direction. This is intentional, I am not going to write it all for you 😉 . Improvements I would make would be not just echo errors out, instead put them into an error array and then iterate through it to display all errors not just the first one found. Also the variables email, username, etc that are defined outside of the reg_horn function aren't accessible to the commands inside the function. That is due to variable scope. Your loop reading the return from the fwrite also seems to bomb out after a single read. If that is intentional, don't use a loop. Nothing says you have to.

    As a starter PHP book I can recommend PHP Visual Quickstart as a very good book with some very nice worked examples.

      Write a Reply...