HOME       >>       Programming

Help With Fopen Script


this is what i have so far, i was just wondering if anyone could help me with a problem im having... i really dont know how to say if a variable doesnt exist, because i want it to either a) choose a random quote or choose quote X where X is $get_['n']

could anyone help me please? this is what i have so far :S

<?php$num = $GET_['n'];if ($num == NULL) {$num = rand(1,400);$open = fopen("http://stirk.simplemedicalsupply.com/bash.php?q=".$num, "r");$lines = file("http://stirk.simplemedicalsupply.com/bash.php?q=".$num);foreach ($lines as $line_num => $line) {echo $line;}} else {$open = fopen("http://stirk.simplemedicalsupply.com/bash.php?q=".$num, "r");$lines = file("http://stirk.simplemedicalsupply.com/bash.php?q=".$num);foreach ($lines as $line_num => $line) {echo $line;}} ?>


I'm presuming the problem is the line:

if ($num == NULL)
as you say you want to check if a variable exists. Try replacing it with this:

if (!isset($num)) {
That basically checks whether anything has given that variable a value, and it's generally considered better and more reliable than using == NULL.


It is better to use

$num = $_REQUEST['n'];
rather than using
$num = $_GET['n'];
since it allows all type of forms - GET forms as well as POST forms.

Also check the fopen operation using urls. This make a long time for file open in some times of high traffic. This makes the script improper working. Use local files only on every time for file operations. Note that the time limit for php execution termination is 30 seconds ( in normal case).


actually, ive gotten it all to work now...

<?phpif(!$_GET['n']) {$num = rand(1,400);$open = fopen("http://stirk.simplemedicalsupply.com/bash.php?q=".$num, "r");$lines = file("http://stirk.simplemedicalsupply.com/bash.php?q=".$num);foreach ($lines as $line_num => $line) {echo $line;}} else {if ($_GET['n']) {$open = fopen("http://stirk.simplemedicalsupply.com/bash.php?q=".$_GET['n'], "r");$lines = file("http://stirk.simplemedicalsupply.com/bash.php?q=".$_GET['n']);foreach ($lines as $line_num => $line) {echo $line;}} }?>

If youd like to see what it does, go to http://forums.xisto.com/no_longer_exists/ for a random quote from bash.org, or
http://forums.xisto.com/no_longer_exists/?n=X where x is a number to view a specific numbered quote


I Would recommend you to use a isset() function.
This checks whether the Variable IS SET or it is not set.

Also you could use the empty() function.
In this function empty() will find return true if the value of the Variable is empty and false if it is not empty.
The things that the empty() function considers empty could be found on the online manual of PHP.NET:

Hope this all helps you out.
I am willing to help if you need some more.


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.