It's ok I've got it sorted now anyway thanks.
illusionist... I'll try to put your mind at rest about what I was doing.
I have a page which loads a random image (showing 6 digits) and a form with a submit button.
The filename of the image is identical to the 6 digit number it contains.
The user is required to input the correct 6 digits in order to progress to the next page. This worked fine simply by checking the filename (minus the extension) against the users input. BUT...
I was using strpos() to do that bit and it still came out as a correct entry if extra digits were added in the user input. Not a big problem but I just wanted it work perfectly so I added and extra if () check to make sure the length matched too.
Here's the code on both pages (it's for downloading polyphonic ringtones in case you wondered).
This is the page with the random image and form
Code: Select all
<?php
/*Creates the variable $file from the GET info on the url of the download link*/
$file=$_GET['file'];
if (empty($file)) {
$file='index';
}
/*Function to get array of all images in directory*/
function listdir()
{
$ImgArray = array();
/*Specifies the directory files are in*/
$handle = opendir('img');
/*Loops in directory to find files*/
while ($file = readdir($handle))
{
/*Reads the file extensions*/
$imgfile = substr($file, -3);
if(($imgfile == 'gif') || ($imgfile == 'jpg') || ($imgfile == 'png') )
{
$ImgArray[] = $file;
}
}
return $ImgArray;
}
/*Shuffles the array*/
$ImgArray = listdir();
shuffle ($ImgArray);
echo '<p align="center">';
echo '<img src="download.gif">';
echo '</p>';
echo '<p class="test">Please enter the code shown in the box below to<br>';
echo 'download <b>';
echo $_GET['n'];
echo '</b>:';
echo '<p align="center">';
echo '<table width="160" border="0">';
echo '<tr>';
echo '<td align="Right">';
/*Displays first image in the shuffled array*/
echo '<img src="img/' . $ImgArray[0] . '">';
echo '<p>';
echo '<form action="download.php" method="post" target="_new">';
echo '<input type="text" name="auth">';
/*Link information and image name are posted in form*/
echo "<input type='hidden' value='" . htmlspecialchars($ImgArray[0]) . "' name='code' />\n";
echo "<input type='hidden' value='" . htmlspecialchars($_GET['maintype']) . "' name='maintype' />\n";
echo "<input type='hidden' value='" . htmlspecialchars($_GET['n']) . "' name='filename' />\n";
echo '<p>';
echo '<input type="submit" name="submit" value="Download">';
echo '</form>';
echo '</td>';
echo '</tr>';
echo '</table>';
?>
(Having problems? <a href="index.php?location=help">Click Here</a>.)
And this is the the page which redirects to the download if the input was correct. It'll be scrappy but I've only just started learning PHP so hey, at least it works
Code: Select all
<?php
$nokstring = 'sorry i've hidden this path because i made the script for a reason';
$codelength = substr($_POST['code'], 0, -4);
if (strpos($_POST['auth'], substr($_POST['code'], 0, -4)) !== false) {
if (strlen($_POST['auth'])== strlen($codelength)) {
if (strpos($_POST['maintype'], 'nokia') !== false) {
header ('Content-type: Audio/Midi');
header ('Content-Disposition: attachment; filename="'.$_POST['filename'].'.mid"');
readfile (''.$nokstring.$_POST['filename'].'.mid');
}
} else {
echo 'Sorry wrong code <p>';
echo 'Please <a href="javascript:window.close();">click here</a> to close this window and try again';
}
} else {
echo 'Sorry wrong code <p>';
echo 'Please <a href="javascript:window.close();">click here</a> to close this window and try again';
}
?>
The variables not defined in these pages have been passed around from previous pages.
Hope this helps you understand what I was doing