I only started learning php a few days ago, so the code might not be best quality, but this seems to work, let me know how it goes max99
freestuff.php
<?php
session_start();
if( isset( $_SESSION['allowed'] ) )
{
echo 'Welcome to the free stuff';
}else{
header( 'Location: emailcapture.php' ) ;
}
?>
emailcapture.php
<?php
session_start();
$successLocation = 'freestuff.php';
$numEmails = 5;
$fileDir = dirname(__FILE__);
$today = getDate();
$filePath = $fileDir.'/emails_'.$today['year'].$today['mon'].$today['mday'].'.txt';
for($i=0; $i < $numEmails; $i+=1)
{
$errors[$i] = '';
if(!isset( $_POST['address'.$i] ) )
{
$emails[$i] = '';
}else{
$emails[$i] = $_POST['address'.$i];
}
}
if( isset( $_POST['btnSubmit'] ) )
{
$errorFound = false;
for( $i=0; $i < $numEmails; $i+=1 )
{
if( !validEmail( $emails[$i] ) )
{
$errorFound = true;
$errors[$i] = "Invalid Email!";
}
}
if( !$errorFound )
{
if( file_exists( $filePath ) )
{
$fhandle = fopen($filePath, 'a') or die("can't open file");
}else{
$fhandle = fopen($filePath, 'w') or die("can't open file");
}
foreach( $emails as $email => $emails )
{
fwrite( $fhandle, "$emails\r\n" );
}
fclose($fhandle);
$_SESSION['allowed'] = true;
header( "Location: $successLocation" ) ;
}
}
function validEmail( $email )
{
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
return true;
} else {
return false;
}
}
?>
<form method="Post">
<?php for($i=0; $i < $numEmails;$i+=1)
{ ?>
Address <?php echo $i+1 ?>:<br />
<input type="text" name="address<?php echo $i ?>" value="<?php echo $emails[$i] ?>"><?php echo $errors[$i] ?><br />
<?php } ?>
<br />
<input type="submit" name="btnSubmit" value="Continue" />
</form>