2013-04-07 55 views
0

使用此代码我想在用户点击按钮时将其变为当它首先验证他们想要下载该文件,如果它们单击是,它会从$减去100个点用户点(然后更新mysql表,但我知道如何做到这一点),我需要帮助的是让代码在有人点击按钮时运行。此外,我想给他们下载没有他们得到他们可以重用的网址。要么没有显示他们的网址或使其成为唯一的一次性代码。在按钮上运行PHP并单击隐藏下载链接

<?php      
        //If submit form was clicked 
        if(isset($_POST['submit'])) { 
         //Server side validation for security purposes 
         if($userpoints >= 100) { 
          mysqli_query($con,"UPDATE users SET points = points - 100 WHERE users.user_name = '$username' LIMIT 1"); 
          $filename = 'ibelongwithyou.mp3'; 
          $filepath = '/home4/saintfiv/downloads/'; 
          header("Content-Length: ".filesize($filepath.$filename)); 
          header("Content-Disposition: attachment; filename=I Belong With You.mp3"); 
          readfile($filepath.$filename); 
         }else { 
          header("Location: index.php?error=1"); 
         } 
        } 
        ?> 
        <form method="post" action="index.php"> 
        <?php 
         if ($userpoints >= 100) { 
          echo '<input type="submit" name="submit" value="100pts">'; 
         } else { 
          echo '<input type="submit" name="submit" value="100pts" disabled title="You need at least 100 points for this download">'; 
         } 
         if(isset($_GET['error']) && $_GET['error'] == 1) { 
          echo "Error, you must have atleast 100points"; 
         } 
        ?> 
        </form> 

有了这个更新后的代码,它似乎仍然在刷新后注册点击提交。还有一些奇怪的事情发生在一堆随机字符中,这些字符只是沿着页面向下。

+0

我想通了,用头下载的一部分,所以我需要的是如何得到它当有人点击该按钮运行帮助最主要的 – 2013-04-07 06:20:35

+0

请提供下载链接,例如路径为你下载...或者,如果它是基于数据库表,然后显示我的下载表字段 – daison12006013 2013-04-07 06:32:04

+0

我更新了我的代码,你能看到发生了什么,我做了一个测试帐户,以便你可以看到发生了什么,saint57records.com/奖励用户名:测试密码:测试。我只有第一轨上的代码,当你点击它时,它会记录减去数据库中的点数,但是我没有下载并且出现所有奇怪的字符。 – 2013-04-07 06:58:36

回答

0

我没有包含一个Javascript/jQuery来验证按钮点击,它的全部由您来包含一个客户端验证。此外,你必须存储会话,也用户像我做了什么未来的SQL语句的USER_ID用户名,我假设你正在使用$ _SESSION [“user_ID的”]找到真正的用户。

<?php 
session_start(); 

function getPoints() { 
    //you must store the user_id atleast for a session or even the username on how you db really done 
    //find the data where the USER_ID is the same as $_SESSION['user_id'] 
    //then we need to get the 'points' field to pass into a variable $userpoints 
    $result = mysqli_query($con,"SELECT * FROM users WHERE user_id = ".$_SESSION['user_id']); 
    $row = mysqli_fetch_array($result); 
    return $row['points']; 
} 
//If submit form was clicked 
if(isset($_POST['submit'])) { 
    //Server side validation for security purposes 
    if(getPoints() >= 100) { 
     mysqli_query($con,"UPDATE users SET points = points - 100 WHERE users.user_name = '$username' LIMIT 1"); 
    }else { 
     header("Location: index.php?error=1"); 
    } 
} 
?> 
<html> 
    <head> 
     <title>Sample Point Downloading</title> 
     <script><!-- Javascript here for Validation --></script> 
    </head> 
    <form method="post" action="index.php"> 
     <?php 
     if (getPoints() >= 100) { 
      echo '<input type="submit" name="submit" value="100pts">'; 
     } else { 
      echo '<input type="submit" name="submit" value="100pts" disabled title="You need at least 100 points for this download">'; 
     } 
     if(isset($_GET['error']) && $_GET['error'] == 1) { 
      echo "Error, you must have atleast 100points"; 
     } 
     ?> 
    </form> 
</html> 
+0

是的,我正在使用$ _SESSION ['user_name']并将其分配给变量$ username,我需要验证什么以及我将如何执行该操作,或者如果您至少可以将我指向正确的方向。 – 2013-04-07 06:33:50

+0

如果你想为浏览器做一些弹出式验证,那么我们称之为** CLIENT SIDE VALIDATION **,通过这样做你必须知道如何使用JavaScript或更好地使用jQuery [http://docs.jquery.com/插件/验证](http://docs.jquery.com/Plugins/validation) – daison12006013 2013-04-07 06:36:25