PDA

View Full Version : Totally clueless!?!?!?


norrash
9-3-06, 01:38 PM
I am attempting to install a counter to record the number of downloads for zip files on my site... example-- layout.zip, blue.zip, green.zip

i have never used SQL before and hate to admit that i am totally clueless about how this works--

i did manage to find the SQL on powwow and have set up a database called zip--- Database is on mysql07.powweb.com

now i am not sure how to proceed----- here are the instructions from the download php file-----

Once you have setup the database, issue the following sql query to
create the table.

the program only requires one small table.
CREATE TABLE `dl_count` (
`file` VARCHAR( 128 ) NOT NULL ,
`count` INT NOT NULL
);
duh what does this mean and how do i do this?
should i rename my database dl_count?

Next upload the download.php script (this file) to a location
on your website. is this simply in the htdoc folder?

Create a web accessable folder in which you
will use to store files. Place all your download files in the
folder. so all the download zip files- red.zip, blue.zip are moved into a separate folder named download or ?

Next alter the variables below to match your database
and folder location.


Usage:
There is no need to setup any filenames in your database, the
script will do it for you upon the first time a file gets downloaded.

Simply create a link to download.php like so:
http://www.yoursite.com/download.php?file=testfile.zip
http://www.creativedistortion.com/download.php?file=testfile.zip

so the DOWNLOAD link becomes the above instead of
http://www.creativedistortion.com/red.zip---correct?


The first time someone access testfile, an entry will be added to the
database and the count will be initialized. They will be transparently
redirected and prompted to download the file.

To display the count on a page:
First you must include the download script on your page by inserting the
following code prior to any calls to the count function.

<?php include("/full/path/to/download.php"); ?>
This is an absolute path, not a URI, on windows it will be similar to C:\inetpub\wwwroot\download.php
and unix /home/username/public_html/download.php
If you try to include using http:// it will not work properly.
major DUH

Then to get a count, simply do the following where you want the number of downloads displayed.
<?php echo showCount("filename.ext"); ?> That will show the count.
Make sure the page with this on it has a .php extension, not .html or anything else.
If that is the case it will not work.
major DUH

//CONFIGURATION SECTION

$FILES_DIR = "/downloads/";
//URI to files
//Include beginning and trailing slash

//This is the web path to your files, not a server path
//Example: www.yoursite.com/folder/files/ will be /folder/files/

www.creativedistortion.com//folder/files--nothing changes here?
//If you wish to serve offsite files, you can use http://www.site.com/downloads/

$MYSQL_USER = "ME"; //The username used to connect to MySQL
$MYSQL_PASS = "MY PASSWORD"; //The MySQL Password for the user
$MYSQL_HOST = "localhost"; //The host to connect to
mysql07.powweb.com

$MYSQL_DB = "drew"; //The database in which the dl_count table is in

$MYSQL_DB = "zip";?
################################################## ############
# Thats IT!! No more configuration required.
################################################## ############


$cnt_sql = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASS);
@mysql_select_db($MYSQL_DB, $cnt_sql);

if(isset($_GET['file'])) {
$file = urlencode($_GET['file']);

if(empty($file)) {
echo "No File Specified";
exit;
}
if(strpos($file, "..") !== FALSE) {
echo "HACK ATTEMPT!";
exit;
}
if(strpos($file, "://") !== FALSE) {
echo "Invalid File";
exit;
}

$cookie = urlencode(str_replace(".", "_", $file)); //cookie fix

$query = "SELECT * FROM dl_count WHERE file = '$file'";
$result = mysql_query($query, $cnt_sql);
if(!$result) {
echo mysql_error();
exit;
}
if(mysql_num_rows($result) == 0) {
//first use of this file
$query = "INSERT INTO dl_count VALUES('$file', 1)";
$result = mysql_query($query, $cnt_sql);
setcookie("dl_" . $cookie, "set", time() + 60*60*24*365);
} else {
if(!isset($_COOKIE['dl_' . $cookie])) {
$query = "UPDATE dl_count SET count = count + 1 WHERE file = '$file'";
$result = mysql_query($query);
setcookie("dl_". $cookie, "set", time() + 60*60*24*365);
}
}

header("Location: " . $FILES_DIR . $file);
}

function showCount($fileID)
{
global $cnt_sql;
$query = "SELECT count FROM dl_count WHERE file = '$fileID'";
$result = mysql_query($query, $cnt_sql);
if(mysql_num_rows($result) == 0) {
return 0;
} else {
$count = mysql_fetch_row($result);
return $count[0];
}
}

?>

many thanks in advance for help on this-- i need kindergarten instructions so please be as specific as possible--

apology for being so clueless--- this is a major learning experience for me--
norrash