PDA

View Full Version : PHP help, please


Jizar ll
03-01-2002, 04:24 PM
HI im using a php script that pics a random line with a gallery url in a txt file, but im getting some weird 404 errors sometimes, like if i click the link 30times, 1 of them will turn 404?<br /><br />here is the code:<br /><br /> </font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: fixed;"> &lt;?php<br /><br />$RANDOM_URL_FILE = &quot;/home/hhujk/htdocs/randomphp/anal.txt&quot;;<br />srand((double)microtime()*1000000);<br />if (file_exists($RANDOM_URL_FILE)) {<br /> $arry = file($RANDOM_URL_FILE);<br /> for($i = 0; $i &lt; sizeof($arry) ; $i++) {<br /> if (preg_match(&quot;/http:/&quot;, $arry[$i]))<br /> $good_arry[$j++] = chop($arry[$i]);<br /> # PHP 4.0 arry_push ($good_arry, $arry[$i]);<br /> }<br /> if ($good_arry) {<br /> header(&quot;Location: &quot;.$good_arry[rand(0, sizeof($good_arry) -1)]);<br /> } else {<br /> echo &quot;error no good URL&quot;;<br /> }<br />} else {<br /> echo &quot;error: can't open $RANDOM_URL_FILE file&quot;;<br />}<br />?&gt; </pre><hr /></blockquote><font size="2" face="Verdana, Arial">I hope some of you can help me out, or maybe send me a better script:)<br /><br />regards<br /><br />Jizar

SixNine
03-01-2002, 04:56 PM
Make sure there are no spaces in anal.txt<br /><br />SixNine

Slackman
03-01-2002, 05:13 PM
$RANDOM_URL_FILE = "/home/hhujk/htdocs/randomphp/anal.txt";<br />$fp = @fopen($RANDOM_URL_FILE);<br />if ($fp){<br /> while(!feof($fp)){<br /> $line++;<br /> $url[$line] = fgets($fp, 4096);<br /> }<br />$rand = rand(1, $line - 1);<br />header("location: $url[$rand]");<br />}<br />else {echo"couldn't open file";}<br /><br />This should be all you need. No need to get fancy it seems..<br />If the file also has blank lines in it, get them out or write some addon so they don't get in the final array.

Slackman
03-01-2002, 05:18 PM
sorry that line:<br />$rand = rand(1, $line - 1);<br />should be:<br />$rand = rand(1, $line);<br /><br />(unless you have a blank line at the end of your file, then you should leave it in)<br /><br />also, just "rand()" will do fine here. no need to seed it for this kind of stuff.

druhix
03-01-2002, 05:33 PM
you guys are drunk <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" /> <br />0 = line 1

Jizar ll
03-01-2002, 05:38 PM
thanks alot for the help, but ima bit confused, can you please sum up how the final script should be:) with no blank lines in the txt file.<br /><br />Jizar.

Slackman
03-01-2002, 05:39 PM
</font><blockquote><font size="1" face="Verdana, Arial">quote:</font><hr /><font size="2" face="Verdana, Arial">Originally posted by Dru Hix:<br /><strong>you guys are drunk <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" /> <br />0 = line 1</strong></font><hr /></blockquote><font size="2" face="Verdana, Arial">Nope, I did "$line++" before the first "fgets()" so "$url[0]" is empty and "$url[1]" contains the first line <img border="0" title="" alt="[Razz]" src="tongue.gif" />

Slackman
03-01-2002, 05:46 PM
</font><blockquote><font size="1" face="Verdana, Arial">quote:</font><hr /><font size="2" face="Verdana, Arial">Originally posted by Jizar ll:<br /><strong>thanks alot for the help, but ima bit confused, can you please sum up how the final script should be:) with no blank lines in the txt file.<br /><br />Jizar.</strong></font><hr /></blockquote><font size="2" face="Verdana, Arial">Just replace the code you qouted with the code I wrote. If it doesn't work, ask me again tomorrow when I'm sober <img border="0" title="" alt="[Wink]" src="wink.gif" />

Slackman
03-01-2002, 05:49 PM
Just put this in a textfile and rename the file to slackman.php that should do it <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" /> <br /><br />&lt;?<br />$RANDOM_URL_FILE = "/home/hhujk/htdocs/randomphp/anal.txt";<br />$fp = @fopen($RANDOM_URL_FILE);<br />if ($fp){<br />while(!feof($fp)){<br />$line++;<br />$url[$line] = fgets($fp, 4096);<br />}<br />$rand = rand(1, $line);<br />header("location: $url[$rand]");<br />}<br />else {echo"Slackman made me do it...";}<br />?&gt;

Jizar ll
03-01-2002, 05:56 PM
Slackman: thanks, but it can find the txt file, its weird the url in the php is correct, but it keep saying "Canīt open file!" ??

Jizar ll
03-01-2002, 06:07 PM
I meant: It CANīT find the txt file! Stupid typo! <img border="0" title="" alt="[Smile]" src="smile.gif" />

Jizar ll
03-01-2002, 07:38 PM
Hmm could anyone please help me get the script working? <img border="0" title="" alt="[Smile]" src="smile.gif" />

redbomb
03-01-2002, 08:59 PM
jizar: if you still need help icq me 63936161

Slackman
03-02-2002, 11:52 AM
Hi Jizar, sorry I gave you broken code, I was a bit drunk last night.<br />This should work:<br /><br /></font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: fixed;">&lt;?<br />$RANDOM_URL_FILE = &quot;/home/hhujk/htdocs/randomphp/anal.txt&quot;;<br />$fp = @fopen($RANDOM_URL_FILE, &quot;r&quot;);<br />if ($fp){<br />while(!feof($fp)){<br />$line++;<br />$url[$line] = fgets($fp, 4096);<br />}<br />$rand = rand(1, $line);<br />header(&quot;location: $url[$rand]&quot;);<br />}<br />else {echo&quot;Kill Slackman&quot;;}<br />?&gt;</pre><hr /></blockquote><font size="2" face="Verdana, Arial">

Jizar ll
03-03-2002, 08:43 AM
thanks alot man:) it works cool...

druhix
03-03-2002, 02:44 PM
you guys are drunk<br />you don't need all that bullshit<br /> </font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: fixed;">&lt;?<br />if(($f=@file(&quot;/home/hhujk/htdocs/randomphp/anal.txt&quot;)))<br />{<br /> for($i=0;$f[$i];$i++){}<br /> header(&quot;location: $f[rand(0,$i)]&quot;);<br />}<br />else {echo&quot;Kill Slackman&quot;;}<br />?&gt;</pre><hr /></blockquote><font size="2" face="Verdana, Arial">ams <img border="0" title="" alt="[Razz]" src="tongue.gif" />

Jizar ll
03-03-2002, 04:32 PM
Dru Hix: I get this error in line 5:<br /><br />Parse error: parse error, expecting `']''