IMPORTANT: Please read our Guide To Quality Writing before you begin posting!

Dismiss Notice
Please note that we are only approving writers from the US, UK and Canada at this time.

PHP Basic Code/Coupon Generator

Discussion in 'Programming' started by fabiof00, Jun 15, 2015.

  1. fabiof00

    fabiof00 New Member

    Joined:
    Jun 15, 2015
    Posts:
    0
    Likes Received:
    0
    Gender:
    Male
    The first thing you should do is change the password, you can access the code/coupon adder by using it as a get variable (e.g http://exa.mp/gen.php?somepassword).The password feature is commented out but you should be able to uncomment it out and add a new password. This script includes:
    • Basic Captcha
    • IP Checker
    • File Protection
    • String Customization
    I wrote it in a rush so its still pretty messy but it works.

    Code:
    <?php
    //Sands Code Generator
    
    //$password = "somepassword";
    $text = ["Coin Code","Generator","Generate Code","You already got a code!","Here is your code: ","Sorry but we are out of stock!","Incorrect Captcha!"];
    
    $special_key = "l02";
    
    $direc = scandir(dirname(__FILE__));
    $product_name = $text[0];
    for ($i = 0; $i <= count($direc); $i++) {
    $val = $direc[$i];
    if (strpos($val,$special_key.'.txt') !== false) {
    $codes = file($val);
    $codes_path = $val;
    } else if (strpos($val,$special_key.'.ips') !== false) {
    $ips = file($val);
    $ips_path = $val;
    }
    }
    
    //Basic Captcha by Sand
    function generateRandomString($length = 10) {
    $characters = '123456789abcdefghijkLmnop';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
    $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
    }
    
    function LoadGif($imgname,$captcha) {
    $im = @imagecreatefromgif($imgname);
    if(!$im) {
    $im = imagecreatetruecolor (60, 30);
    $bgc = imagecolorallocate ($im, rand(100,255), rand(100,255), rand(100,255));
    $tc = imagecolorallocate ($im, rand(0,100), rand(0,100), rand(0,100));
    imagefilledrectangle ($im, 0, 0, 60, 30, $bgc);
    for ($i = 1; $i <= 100; $i++) {
    $ta = imagecolorallocate ($im, rand(100,255), rand(100,255), rand(100,255));
    imagestring ($im, 1, rand(0,50), rand(0,20), generateRandomString(10), $ta);
    }
    imagestring ($im, 20, 5, 5, $captcha, $tc);
    }
    return $im;
    }
    
    function captchy(){
    $captcha = $_POST['captchy'];
    $file = file("captcha.txt");
    if(isset($captcha)){
    $hash = md5($_SERVER['REMOTE_ADDR'].":".$captcha)."\n";
    if (in_array($hash, $file)) {
    return true;
    } else {
    return false;
    }
    }
    }
    
    function newCaptchy() {
    $captcha = generateRandomString(6);
    $img = LoadGif('image.gif',$captcha);
    $file = "captcha.txt";
    if(count(file($file))>=5){
    file_put_contents($file,preg_replace('/^.+\n/', '', file_get_contents($file)));
    }
    ob_start();
    imagegif($img);
    $imagedata = ob_get_contents();
    ob_end_clean();
    $hash = md5($_SERVER['REMOTE_ADDR'].":".$captcha);
    file_put_contents($file,file_get_contents($file).$hash."\n");
    $base64 = 'data:image/gif;base64,'.base64_encode($imagedata);
    echo "<input style='background:transparent;border:2px solid transparent;border-radius:8px;vertical-align:top;line-height:30px;width:45;' maxlength='6' autocomplete='off' type='text' name='captchy' placeholder='Solve it'/><img style='border:3px solid white;border-radius:8px;' src='".$base64."'><p>";
    imagedestroy($img);
    }
    //Basic Captcha End
    
    $submit = $_POST['code-submit'];
    $passset = $_GET[$password];
    if(empty($codes_path)){
    $passset = true;
    }
    
    if(isset($passset)){
    if (isset($_SERVER['HTTP_USER_AGENT'])){
    @$codes_text = file_get_contents($codes_path);
    $text = "<h3>Edit Codes</h3><p><form action='' method='POST' autocomplete='off'>Codes:<br><textarea autocomplete='off' placeholder='Paste your codes in here' type='text' name='codes' id='".rand(0,99999999999999)."' cols='40' rows='10'>".$codes_text."</textarea><br><input type='submit' value='Add Codes' name='code-submit' /></form>";
    if(empty($submit)){
    echo $text;
    die();
    } else {
    file_put_contents("captcha.txt","");
    if (empty($codes_path)){
    file_put_contents(md5(rand(0,99999)).$special_key.".txt",$_POST['codes']);
    } else {
    file_put_contents($codes_path,$_POST['codes']);
    }
    if(empty($ips_path)){
    file_put_contents(md5(rand(0,99999)).$special_key.".ips","");
    }
    echo "Codes Added!<p>";
    $title = $product_name." Generator";
    }
    }
    } else {
    $title = $product_name." ".$text[1];
    }
    
    if (isset($_SERVER['HTTP_USER_AGENT'])){
    
    echo "<title>".$title."</title>";
    echo "<font size=5>".$title."</font>";
    echo "<form action='' method='POST'>";
    newCaptchy();
    ?><style>.generate {-moz-box-shadow:inset 0px 1px 0px -4px #ffffff;-webkit-box-shadow:inset 0px 1px 0px -4px #ffffff;box-shadow:inset 0px 1px 0px -4px #ffffff;background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #f9f9f9), color-stop(1, #e9e9e9));background:-moz-linear-gradient(top, #f9f9f9 5%, #e9e9e9 100%);background:-webkit-linear-gradient(top, #f9f9f9 5%, #e9e9e9 100%);background:-o-linear-gradient(top, #f9f9f9 5%, #e9e9e9 100%);background:-ms-linear-gradient(top, #f9f9f9 5%, #e9e9e9 100%);background:linear-gradient(to bottom, #f9f9f9 5%, #e9e9e9 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#f9f9f9', endColorstr='#e9e9e9',GradientType=0);background-color:#f9f9f9;-moz-border-radius:42px;-webkit-border-radius:42px;border-radius:42px;border:1px solid #dcdcdc;display:inline-block;cursor:pointer;color:#666666;font-family:Arial;font-size:10px;height:18px;font-weight:bold;padding:0px 11px;text-decoration:none;text-shadow:0px 0px 0px #ffffff;}.generate:hover {background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #e9e9e9), color-stop(1, #f9f9f9));background:-moz-linear-gradient(top, #e9e9e9 5%, #f9f9f9 100%);background:-webkit-linear-gradient(top, #e9e9e9 5%, #f9f9f9 100%);background:-o-linear-gradient(top, #e9e9e9 5%, #f9f9f9 100%);background:-ms-linear-gradient(top, #e9e9e9 5%, #f9f9f9 100%);background:linear-gradient(to bottom, #e9e9e9 5%, #f9f9f9 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#e9e9e9', endColorstr='#f9f9f9',GradientType=0);background-color:#e9e9e9;}.generate:active {position:relative;top:1px;}</style><input type="submit" name='generate' class="generate" value="Generate"/><?php
    echo "</form>";
    
    if(isset($_POST['generate'])){if(captchy()){
    if(in_array($_SERVER['REMOTE_ADDR']."\n",$ips)){
    die("<font color='red' size='2'>".$text[3]."</font>");
    } else {
    $ips_val = file_get_contents($ips_path);
    file_put_contents($ips_path,$ips_val.$_SERVER['REMOTE_ADDR']."\n");
    if(empty($codes)){
    die("<font color='red' size='2'>".$text[5]."</font>");
    }
    $client_code = $codes[rand(0,count($codes))];
    $codes_val = str_replace($client_code,"",file_get_contents($codes_path));
    file_put_contents($codes_path,$codes_val);
    echo "<font size='2' color='lime'>".$text[4]."</font><font size='2'>".$client_code;
    }
    } else {
    echo "<font color='red' size='2'>".$text[6];
    }}
    echo "</font></div>";
    }
    ?>
    
     

Share This Page