php密钥赠品脚本不起作用

时间:2014-10-28 08:47:35

标签: php

我正在尝试制作一个关键的赠品脚本,以便在有人在输入中输入邮件时给出密钥。我走了很远,但现在我有1个问题,当它发送邮件我应该发给你钥匙但它没有,它只是发送文本"你的代码是:"

它在" $ message"中发送密钥每当它发送它时,它应该运行函数dispres并从数据库中获取密钥。

有人能看出问题所在吗?

<?php
     //fill in mail

    echo "
    <form method='post' action=" . $_SERVER['PHP_SELF'] . ">
        Email: <input name='email'></input><br>
        <input type='submit' value='Get your key' name='submit'> </input><br><br>
    </form>";

        if(empty($_POST["email"]))
        {
            echo "Please enter an email adress.";
        }
        else{

            //mail key to input mail
            $to      = $_POST["email"];
            $subject = 'TEST';
            $message = 'Your code is:' . dispres();
            $headers = 'niks';

            mail($to, $subject, $message);

            echo "Your code has been sent to your email: " . $_POST["email"] . " \r";
            echo dispres();
        }



    function dispres(){

    //database connect
    $db_host = 'localhost';
    $db_user = 'root';
    $db_pwd = 'password';

    $database = 'keys';
    $table = 'keytest';

    if (!mysql_connect($db_host, $db_user, $db_pwd))
        die("Can't connect to database");

    if (!mysql_select_db($database))
        die("Can't select database");


    //grab random key from database

    $result = mysql_query("SELECT * FROM {$table} order by RAND() LIMIT 1");

    $row = mysql_fetch_row($result);

    $result = implode('|',$row);


    echo $result;






    //delete key from database

    //mysqli_query("DELETE FROM keytest WHERE name='$cell1'");

    //remember ip adress for 1 use only
    }
    ?>

1 个答案:

答案 0 :(得分:0)

  1. 向dispres()函数添加一个return语句。
  2. 只调用一次dispres(),每个用户只能获得一个密钥。
  3. if (empty($_POST["email"])) {
        echo "Please enter an email adress.";
    } else {
        // get key from database
        $key = dispres();
    
        //mail key to input mail
        $to      = $_POST["email"];
        $subject = 'TEST';
        $message = 'Your code is:' . $key;
        $headers = 'niks';
    
        mail($to, $subject, $message);
    
        echo "Your code has been sent to your email: " . $_POST["email"] . " \r";
        echo $key;
    }
    
    function dispres() {
        // ...
    
        // replace:
        // echo $result;
        // by:
        return $result;
    }