Forum Moderators: coopster
I have been making up PHP scripts for a little while now, and as a project for a new site I have built, I needed some forms that an admin for the site could fill in so certain pages could be made/edited/adjusted.
What should have been the simplest script of them all is the one that is failing to function as desired. The script has a client-side page called MakeGallery.php - this contains a form and 2 text boxes to fill in, one for the year and one for the topic number. The script is meant to create a script that pulls out messages from a set topic in a PHPBB forum, hence why the Topic number is required.
MAKEGALLERY.PHP:
=====
<form method="post" action="phpCreateGallery.php">
Enter the <b>year</b> this gallery is for:
<br /><input type="text" name="Year" size="35" style="color: #171717; border: 1px solid #303030; background-color: #999999" value="" maxlength="4" /><br /><br />
Enter the <b>topic ID number</b> of the POTM gallery thread on the forum for this year<br />
(look for the number after the bold text shown in this example: viewtopic.php<b>?t=</b>100)<br />
<input type="text" name="TID" id="TID" size="35" style="color: #171717; border: 1px solid #303030; background-color: #999999" value="" maxlength="4" /><br /><br />
<input type="submit" value="Create the file" style="color: #171717; border: 1px solid #303030; background-color: #999999" />
</form>
=====
The file phpCreateGallery.php is supposed to take the Year and TID variables sent in the above form, and create a new file called "pullposts-potm"{Year}".php", and insert the post-pulling script with the Topic ID number required which was set in the TID variable.
I also wrote in failsafes so that the script dies if a part of the script did not work
PHPCREATEGALLERY.PHP:
=====
<?php
$Year = $_POST['Year'];
$TID = $_POST['TID'];
echo $TID . "<br>";
if(isset($_GET['TID']))
{$TID = $_GET['TID'];
switch($TID)
{case NF:
{echo '';
break;}
default:
{echo '';
break;}
}
}
else { die('<html><head><title>Please wait</title><link rel="prefetch" href="MakeGallery.php?N=NF" />
<meta http-equiv="Refresh" content="3; url=MakeGallery.php?N=NF&number=" . $TID . "" />
<script>window.location="MakeGallery.php?N=NF"></script></head><body>' . $Year . ' | ' . $TID . '<center>Please wait while the script works. Page should automatically redirect in about 3 seconds.<br /><a href="MakeGallery.php?N=NF">If page does not redirect, click this text</a></center></body></html>'); }
$times = substr_count($_SERVER['PHP_SELF'],"/");
$rootaccess = "";
$i = 1;
while ($i < $times) {
$rootaccess .= "../";
$i++;
}
function replace_TID($text, $xhtml = true)
{
$tags = array(
'#TopicT = \'5\'#si' => 'TopicT = ' . $TID . '', );
foreach ($tags AS $search => $replace)
{
$text = preg_replace($search, $replace, $text);
}
return $text;
}
$File = implode('', file($rootaccess.'pullposts-potm2009.php'));
$parse = replace_TID($File);
$fh = fopen($rootaccess."pullposts-potm" . $Year . ".php", "w");
if($fh==false)
{die('<html><head><title>Please wait</title><link rel="prefetch" href="MakeGallery.php?N=NF" />
<meta http-equiv="Refresh" content="3; url=MakeGallery.php?N=NF&number=" . $TID . "" />
<script>window.location="MakeGallery.php?N=NF"></script></head><body><center>Please wait while the script works. Page should automatically redirect in about 3 seconds.<br /><a href="MakeGallery.php?N=NF">If page does not redirect, click this text</a></center></body></html>');}
$stringData = $parse;
fwrite($fh, $stringData);
$NewFile = $fh;
echo '<html><head><title>Please wait</title><link rel="prefetch" href="MakeGallery.php?N=FC&F=' . $NewFile . '" />
<meta http-equiv="Refresh" content="3; url=MakeGallery.php?N=FC&F=' . $NewFile . '&number=" . $TID . "" />
<script>window.location="MakeGallery.php?N=FC&F=' . $fh . '"></script></head><body><center>Please wait while the script works. Page should automatically redirect in about 3 seconds.<br /><a href="MakeGallery.php?N=FC&F=' . $NewFile . '">If page does not redirect, click this text</a></center></body></html>';
fclose($fh);
?>
=====
Everytime I run the script, it always defaults to the first DIE statement, as thought the variable TID is not set - however the ECHO command I have placed in there for testing always shows up the correct value (meaning that TID is set!)
This script opens a file called pullposts-potm2009.php, looks for a line "TopicT = '5'" and is supposed to replace the 5 with whatever TID is meant to be. However this never happens, even if the ECHO command at the top of the script shows that TID is set! Even the redirect line within the DIE (or the final redirect if the script is a success) where there is "&number=' . &TID . '" still shows as blank, nothing after the = sign. The new file is written fine, and is given the correct Year number, and the script is inserted with the "replace_TID" php function working to take out the number 5 - but because the variable TID isn't getting fully passed through, that number 5 doesn't get replaced with anything.
In addition, when I change things so that the script completes so that the final redirect works, the variable $NewFile is not getting passed through, and instead is changed to something like "Resource #3"
Feel free to test the scripts yourselves, as I'm completely stumped on this. Is anyone here able to work out why the variable TID is not getting passed fully through the script?
I have tried using both POST and GET, and neither work.
(final note:)
What is really strange is in this excerpt from the first failsafe DIE command
=====
<link rel="prefetch" href="MakeGallery.php?N=NF" />
<meta http-equiv="Refresh" content="3; url=MakeGallery.php?N=NF&number=" . $TID . "" />
<script>window.location="MakeGallery.php?N=NF"></script></head><body>' . $Year . ' | ' . $TID . '<center>Please wait while the script works.
=====
- Where it has the url containing "&number=" . &TID . "", the variable TID returns blank (not set), but within the body text is "' . $Year . ' | ' . $TID . '", the TID variable returns the correct value!
thanks in advance
Welcome to webmaster world!
Just glancing over your script I notice this:-
switch($TID)
{
case NF:
{echo '';
break;}
default:
{echo '';
break;}
switch($TID)
{//opening brace
case "NF":echo "";
break;
default:
echo "";
break;
}//closing brace
Hope that this helps,
Cheers,
MRb
function replace_TID($text, $xhtml = true) {
$tags = array(
'#TopicT = \'5\'#si' => 'TopicT = ' . $TID . '', );
// etc
}
Within a function, $TID is not available to the function. I'm surprised you don't get an undefined variable here. Add this:
function replace_TID($text, $xhtml = true)
{
global $TID;
.....
And it now will be. Better yet, to NOT use globals,
$parse = replace_TID($TID,$File);
function replace_TID($this_tid, $text, $xhtml = true) {
$tags = array(
'#TopicT = \'5\'#si' => 'TopicT = ' . $this_tid . '', );
// etc
}
I "kind of" see what's going on here, but doing this is dangerous as it changes the value of $TID as the program executes:
$TID = $_POST['TID'];
if(isset($_GET['TID']))
{$TID = $_GET['TID'];
A way to get that under control is
$post_tid=$get_tid=NULL;
$post_tid = $_POST['TID'];
if(isset($_GET['TID'])) {
$get_tid = $_GET['TID'];
}
$TID=($post_tid)?$post_tid:$get_tid;
if (! ($TID > 0)) { die("TID is not set."; }
I use > 0 because you should never have a value of 0 for this, if you do, something is wrong.