Hello, I am still working on my Demo uploader site and would like to add a functionality which only allows users to upload 5 files within any given 24hour period. If they have uploaded 5 files, I want to make the form 'hidden' on the page.
I have seen a simple example of retrieving a users IP on php.net and know it is something to do with the $_SERVER global.
What I need to know is how would I integrate this into my current code:
My examples.php(main page):
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>php form</title>
<link rel="stylesheet" type="text/css" href="styles/style.css" />
</head>
<body>
<div id="container">
<img src="images/mainbanner_v3.png" alt="DXDU Banner" title="Deus Ex Demo Uploader Logo" id="logo" border="0" />
<?php define("MAX_FILE_SIZE", 10485760); ?>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>Upload Your Deus Ex Demos</legend>
<p><label for="title">Title:</label><input type="text" name="title" maxlength="40" size="45" />
<p><label for="description">Description:</label><textarea cols="34" maxlength="170" rows="5" name="description"></textarea></p>
<p><label for="browse">Browse:</label><input type="file" name="file_upload" /></p>
<p><label for="submit">Upload:</label><input type="submit" name="submit" value="Submit" /></p>
</fieldset>
</form>
<div id="guidelineDiv">
<pre>
<u>Guidelines:</u>
1. You can only post demo files with the extension (.dem).
2. You can only post demo files which are 10mb or less.
<span class="bold">WARNING:</span><span class="white"> Measures are in place to prevent users uploading
anything but demo files. Persistance in trying to upload
malicious files will result in the user being banned from
uploading files, allowing them only to download files.
Should you repeatedly experience problems with uploading,
please email me at <span class="email">flipmodeskwaud@hotmail.co.uk</span> also
proving the specific error message, so that the issue
can be debugged quicker. Thank you. Please enjoy</span>
</pre>
</div>
<?php
if(isset($_SESSION['answer'])){
echo "<p class='messages'>{$_SESSION['answer']}</p>";
session_destroy();
}
else{
$_SESSION['answer'] = "undefine";
session_destroy();
}
?>
<?php if(!empty($message)){ echo "<p>{$message}</p>"; } ?>
<hr id="ruleHeader" noshade="noshade" size="4" />
<table id="results">
<tr>
<th>Demo Name</th>
<th>Title</th>
<th>Description</th>
<th>Size</th>
<th>Date</th>
</tr>
<?php
require("connect.php");
$query = mysql_query("SELECT * FROM fileinfo");
while($row = mysql_fetch_array($query)){
echo '<tr><td><a href="download.php?id='.urlencode($row['id']).'&name='.urlencode($row['name']).'">'.$row['name'].'</a></td><td> '.$row['title'].'</td><td> '.$row['description'].'</td><td> '.$row['size'].'</td><td> '.$row['date'].'</td></tr>';
}
?>
</table>
</div>
</body>
</html>
My upload.php (action script):
<?php
session_start();
define("MAX_FILE_SIZE", 10485760);
$upload_errors = array(
UPLOAD_ERR_OK => "No Errors.",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL => "Partial upload.",
UPLOAD_ERR_NO_FILE => "No file.",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
);
/*FUNCTION TO SANITIZE USER INPUT. RETURNS USER INPUT + FILENAME VARIABLES STRIPPED OF ANY SPECIAL CHARACTERS*/
function check_input($data) {
$illegalChars = array('!','@','#','$','%','^','&','*','(',')','+','=','-','[',']','.',';',',','/','{','}','|','"',':','<','>','?');
$data = str_replace($illegalChars,'',$data);
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES);
$data = mysql_real_escape_string($data);
return $data;
}
/*FUNCTION THAT AMENDS A 1-2 DIGIT NUMBER ONTO THE END OF A FILENAME, IF THAT FILENAME ALREADY EXISTED.*/
function editName($data){
$info = pathinfo($data);//retrieves info on the file path.
$data = basename($data,'.'.$info['extension']);//returns just the filename - without extension.
$data = $data.rand(0, 99);//concatenates a random 2 digit number onto the end of the file name.
$data = $data.'.'.$info['extension'];//concatenates the extension of the file back onto the new filename.
return $data;//returns the new filename.
}
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(is_uploaded_file($_FILES['file_upload']['tmp_name'])){
if($_FILES['file_upload']['error'] === 0){//if the uploaded file uploaded with no error....
$title = check_input($_POST['title']);
$description = check_input($_POST['description']);
$name = basename($_FILES['file_upload']['name']);//full file name and extension.
$type = $_FILES['file_upload']['type'];//file type eg. application/octet-stream..
$size = $_FILES['file_upload']['size'];//file size in bytes.
$tmpname = $_FILES['file_upload']['tmp_name'];//temporary name on server before being moved.
$date = date('d/m/Y');//date - format dd/mm/yyyy.
$c = substr_count($name, '.');//counts how many occurences of '.'
$dir = "files";//directory name for target file.
$ext = pathinfo($name);//Array..information about the file..
$ext = $ext['extension'];//stores the file extension in $ext...
if($c === 1){
while(file_exists($dir.'/'.$name)){//while a file exists in that directory with the same name as the given file...
$name = editName($name);//then amend the file name.
}
if($type == 'application/octet-stream' && $ext == 'dem' && $size <= MAX_FILE_SIZE){
if(move_uploaded_file($tmpname, $dir."/".$name)){//if the file moved successfully, then insert information to database.
require("connect.php");
$query = mysql_query("INSERT INTO fileinfo VALUES('', '{$name}', '{$title}', '{$description}', '{$size}', '{$date}')");
if(!$query){
$_SESSION['answer'] = "There was a MYSQL error.";
header("Location: examples.php");
}
else{
$_SESSION['answer'] = "Your file uploaded successfully.";//create session variable to use in examples.php--only on success
header("Location: examples.php");//redirects user to the examples.php page.
}
}
else{
$_SESSION['answer'] = "Error: Couldnt move file. Please try again later.";
header("Location: examples.php");
}
}
else{
$_SESSION['answer'] = "Error: Please make sure to upload only demo files which are 10 MB or less.";
header("Location: examples.php");
}
}
else{
$_SESSION['answer'] = "Error: Corrupted file.";
header("Location: examples.php");
}
}
else{
$error = $_FILES['file_upload']['error'];
$message = $upload_errors[$error];
}
}
else{
$_SESSION['answer'] = "Error: Please try uploading again.";
header("Location: examples.php");
}
}
else{
$_SESSION['answer'] = "Error with the HTTP service.";
header("Location: examples.php");
}
?>
Any advice, links or guidance will be greatly appreciated as I am completely lost..
Regards,
Labtec