Posting data between php pages (Authorization code)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Posting data between php pages (Authorization code)

Post by Chris Corbyn »

Hi guys,

I'm a bit of a newbie to php but I am fast to learn and v interested.

I'm building a website where users can download polyphonic ringtones for free. In order to prevent leeching etc I wanted to put one of those forms where you are asked to key in the six digit code shown in the image above.

I didn't know where to start so I made lots of "6 digit code containing" gifs and named them according to the relevant code.

I then created a page where an array is told to loop through the image directory and then shuffle the contents and display the first one in the suffled array:

Code: Select all

<?php

/*Function to get array of all images in directory*/

function listdir()
&#123;
	$ImgArray = array();

/*Specifies the directory files are in*/
        
	$handle = opendir('img');

/*Loops in directory to find files*/

	while ($file = readdir($handle))
	&#123;
		$imgfile = substr($file, -3);

		if(($imgfile == 'gif') || ($imgfile == 'jpg') || ($imgfile == 'png') )
		&#123;
			$ImgArray&#1111;] = $file;
		&#125;
	&#125;

	return $ImgArray;
&#125;

/*Decides which image to use*/

$ImgArray = listdir();
shuffle ($ImgArray);
/*Image display*/

echo '<img src="img/' . $ImgArray&#1111;0] . '">';
?>
I need to place a form in this same page too (I'll come back to that in a mo).

Ok, now the idea is that I have two cells side by side. One contains a php document listing the files available (as links), the other will load the random image page (codeloader.php) when a file is clicked.

I need to create a code to do this...

When a file link is clicked in the left hand cell it will prompt the right hand cell to load codeloader.php but I also need codeloader.php to know the link source in preparation for activating the download. I figured I'd need to use some sort of POST command inside my link to send the data to codeloader.php.

Codeloader.php will contain a form (i have no problem making forms and sending the inputted data to another php and using the if and else commands to do different things depending on form input) but I need it to send these three things to a file called download.php:

1. The filename of the picture my script loaded (without the .gif extension)
2. The link source (so that I can make an if command to redirect to the link)
3. The users input (easy)

How do I get my form to post all of these things simultaneously. My major problem is getting the link source data to be passed from page to page and the image filename to passed on (i'm not sure it's possible because I shuffled the array)

download.php then checks if the user input matches the image filename{then redirects to link if so} or
else {displays an error message}.

Please help :-(

I don't really want the code written for me.. I just need pointing in the right direction.

Kind Regards,

Chris
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hi again,

Just to let you know I got the authorization script working in the end.

It was easy to pass the variables from page to page without sessions in the end.

I just added ?file=1 or ?file=2 to all of my links and then used the GET array in my codeloader.php to create a new variable with it.

Then, using a hidden submit

Code: Select all

echo "<input type='hidden' value='" . htmlspecialchars($ImgArray&#1111;0]) . "' name='code' />\n";
echo "<input type='hidden' value='" . htmlspecialchars($file) . "' name='file' />\n";
in my form I managed to send this variable along with the image filename to download.php using POST.

download.php then checks the users input in the form and sees if it matches the filename of the image (minus the extension) using

Code: Select all

if (strpos($_POST&#1111;'auth'], substr($_POST&#1111;'code'], 0, -4)) !== false) &#123;

/*Specifies where each individual tone is located*/

	switch ($tone) &#123; /*I've put my url's in here for each $tone variable and set the relevant redirect path*/
                                       &#125;
                 &#125;
This worked fine.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Excellent. Well done for sussing it :)
Post Reply