Click to See Complete Forum and Search --> : Validate a URL before loading it!
tiggerific
06-18-2003, 01:15 PM
Ok, Pyro has sweet-talked me into 'learning PHP' ;)
The problem is:
My page contains an iframe which loads the source file based on the variable passed to the page. Before I update the iframe with the source, I want to check that the variable passed will generate a valid (live) url to be loaded, then go ahead and load it.
If not valid, I will load my personal 'file not found' page or something similar.
Thanks.
Gotta love PHP... :)
Here is some basic code. It will take the URL you input into a textbox, check if it is a valid page, and then display it in an iframe. If you explain to me how you are passing the value to the page, I will get you some updated code.
Demo at http://www.infinitypages.com/temp/loadpage.php
<?PHP
$x = 0; #set #x to zero
if (isset($_POST["submit"])) { #if form has been submitted
$url = $_POST["url"]; #set $url to be the value of the input area "url"
$file = @file ($url); #url to open. $file equals the url inputted
if ($file) { #if $file was found
$x = 1; #set $x to one for later use (ie. file was found)
}
else { #if file was not found
$x = 2; #set $x to two for later use (ie. file was not foune)
}
}
?>
<html>
<head>
</head>
<body>
<form action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="post">
URL: <input type="text" name="url">
<input type="submit" name="submit" value="Submit">
</form>
<?PHP
if ($x == 1) { #if $x equals one (ie, if file was found)
echo "<iframe src=\"$url\"></iframe>"; #display the iframe with the file loaded.
}
else if ($x == 2) { #if $x equals two (ie. if file was not foune)
echo "Sorry, that URL is invalid"; #display an error message if the URL is invalid.
}
?>
</body>
</html>
tiggerific
06-18-2003, 01:30 PM
as i said - i'm just fudging through it..
when a menu item is selected, the href simply points to the appropriate doc and source:
'page.htm?xxx'.
When I open 'page.htm' I extract the 'xxx' as a variable then update the iframe on the page using top.framename.location='xxx' (+.htm).
It all works ok, but I just wanted to tidy it up a little to allow for outdated links and such, instead of getting a nasty little dnserror page or something in my iframe.
now, remember, php is not my strong suit :).
I should just save the php as 'whatever.php' and it goes in my 'httpdocs' or it has to go to a specific php directory? or am i just asking you something that only my host will know???
and going by your code, is the php simply at the top of the same document, or is that 2 different docs?? (Big L for learner!)
Ok, I was assumbing you were doing something like that, so try this code:
<?PHP
$x = 0; #set #x to zero
if (isset($_GET["url"])) { #if a query string is set
$url = $_GET["url"]; #set $url to be the value of the input area "url"
$file = @file ($url); #url to open. $file equals the url inputted
if ($file) { #if $file was found
$x = 1; #set $x to one for later use (ie. file was found)
}
else { #if file was not found
$x = 2; #set $x to two for later use (ie. file was not foune)
}
}
?>
<html>
<head>
</head>
<body>
<a href="yourpage.php?url=http://www.php.net">Load http://www.php.net</a>
<?PHP
if ($x == 1) { #if $x equals one (ie, if file was found)
echo "<iframe src=\"$url\"></iframe>"; #display the iframe with the file loaded.
}
else if ($x == 2) { #if $x equals two (ie. if file was not foune)
echo "Sorry, that URL is invalid"; #display an error message if the URL is invalid.
}
?>
</body>
</html>
Ok.
1. yes, just save this as yourpage.php (if you name it differently, change the link to point to your page. ( <a href="yourpage.php?url=http://www.php.net">Load http://www.php.net</a> ) You can put PHP pages in any directory that you would like.
2.Yes, it is at the top of the document. One file.
tiggerific
06-18-2003, 01:38 PM
Cool :cool:
I'm not going to try it right now though - it's 4.30am and I've gotta get up in a few hours!
I've saved the code and I'll give it a try tomorrow. Thanks muchly for all your help.
ya know i thought you'd be big and tough with a name like Pyro, but you're actually kinda sweet :)
Robyn.
hehe... I try to be nice... ;)
Let me know how you get on with the code, and if there is anything else I can do to help you...
G'night!
Ok. Lets try this again.
Here is the complete code w/ php, javascript and html form. ('had to go to work this AM)
This method allows you to check the url before loading into the iFrame.
Thought that was basically what you were looking for.
The javascript has three routines:
checkURL() - Ask the server if the URL/page exists,
processReturn() - If no, alert the user and if yes, load the URL into the iFrame, and
noResponse() - If for some reason the server is not responding, tell the user.
The PHP is also simple:
If the URL exists, send back a 1x1 image file, and
If not, return a 2x2 image file.
The user never leaves the original page.
Add one javascript routine and you can disable the back button enabled by the iFrame loads.
Simple enough, I thought.
Hers is an example: file=test is valid most others are not, say file=junk.
...and here is the code.
<?PHP
if (isset($_GET["url"])) { #if a query string is set
$url = $_GET["url"]; #set $url to be the value of the input area "url"
$file = @file ($url); #url to open. $file equals the url inputted
if ($file) { #if $file was found
$x = 1; #set $x to one for later use (ie. file was found)
} else { #if file was not found
$x = 2; #set $x to two for later use (ie. file was not foune)
}
// create an image of size 1x1 if URL exiats or 2x2 if it does not
$img = imagecreate($x,$x) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($img, 255, 255, 255);
header("Content-type: image/png");
imagepng($img); // Send the image to the the user
exit; //done
}
?>
<!-- PHP above and your HTML below -->
<HTML>
<HEAD>
<TITLE>URL Entry Page</title>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
var imgObj=null, rTimer=null, attempts=3
function checkURL() {
var flag=new Date()
if(rTimer==null) { // disable submit if in process
imgObj=new Image()
imgObj.onload=function(evt){processReturn()}
imgObj.src= "http://www.fremontseattle.com/greg/testPage.php?flag="+flag.valueOf()+"&url="+document.myForm.myFile.value+".html"
rTimer=setTimeout("noResponse()",4999); // 5 seconds for timeout
}
return false;
}
function processReturn() {
if(rTimer!=null){clearTimeout(rTimer);rTimer=null}
url = document.myForm.myFile.value+".html"
if(imgObj.width == 1) {
document.getElementById('myIframe').src=url
} else {
alert("ERROR:\nFile "+url+".html does not exist. \nPlease check your entry data. ")
}
}
function noResponse() {
imgObj=rTimer=null
if(--attempts)checkURL()
else alert("Server Error 1002 - Timeout. ")
}
</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<BR><BR>
<H1>URL Entry/Test Form</H1>
<br><br>
<form name="myForm" action="" onsubmit="return checkURL();">
<input type="text" size="30" name="myFile">
<input type="submit" value=" Get File ">
</form>
<strong>(valid entry: test, most all else is invalid)</strong><br>
<br><hr><br><iFrame id="myIframe" width="90%" height="200"></iFrame><br><br>
</CENTER>
</BODY>
</HTML>
...and a test URL
Example (http://www.fremontseattle.com/greg/testPage.php)
Work gets in the way some days.
Sorry if this is too late. :(
Out of curiosity... Why use javascript, when it can easily be done with all PHP (as per my example)? 13% of users have javascript disabled, so it seems foolish to rely on javascript, when it is clearly has a server side solution (and, server side code is needed anyway!) Also, by trying to do this with javascript, the code is more complex than it has to be...
Hmm...
Basically, 'cause the OP was under JavaScript and I responded before I went to work.
Upon arrival, all has changed and I was simply expanding on my response to the OP.
This RSP method has, IMHO, advantage when you want to validate form entries against the server prior to submitting.
And, for my information, why do some not have JavaScript?
Well, I stand by my opinion that a server side method is the best.
Javascript is disabled for various reasons. The most common is probably that people are starting to get tired of all the popup ads and other misuses of javascript. Here's a link to the stats: http://www.thecounter.com/stats/2003/May/javas.php
Well, that's a philosophical issue.
Do those same people unplug their telephones to avoid unsolicited calls?
People are strange at times, all but you and me I'm sure.
Thanks.
lol... Whether or not it is a philosophical issue, it is a reality. ;)
Yeah.
...and I bet they they still have 8-track players in the dash!!
tiggerific
06-18-2003, 11:10 PM
OK you guys - no squabbling!
I'm just attempting to get the code (Pyro's original one) working now. Can you tell me if there's any easy way to run php locally so I can test it properly?
Also, I'm not even clear on which url's I'm supposed to be using. For example, this is how my page flow goes:
1. user opens index.htm and selects option aaafrom the menu
2. menu.js says to load pages.htm?aaa !!It's done this way because it was created with the xaramenu maker!!
3. pages.htm extracts 'aaa' and creates a variable called menuitem
4. blank iframe is created, then iframe source is updated by specifying url=menuitem.
Told you, I'm pretty thick when it comes to putting all this together (and if you want to see where i'm at so far it's at www.tigger2.com)
Now, boys, which method should I be attempting to use??
Ok, I'm going to run through this and try to answer your questions...
First of all, if you want to test PHP scripts locally, you will have to have a testing server installed. I use Abyss Web Server ( http://www.aprelium.com/abyssws/ ) with PHP4.3.1 ( http://www.php.net/downloads.php )
What you are going to want to do is switch menu.js to spit out URLs more like this... pages.php?url=somepage.htm Note that any pages using PHP must end with a .php extention.
Now, pages.php will extract the page, as described above (my second bit of code). the <iframe> can be created if a page is found and a custom error can be displayed if not.
tiggerific
06-19-2003, 07:51 AM
OK!
So I should:
1. change pages.htm to pages.php
2. add php code as you've written it
3. use the second piece of php code to test the url before i call it in the frame source script
4. change menu.js to send pages.php?url=xxx.htm (can I add the relative path here or do that when i work with the variable back on the pages.php script?)
5. in pages.php complete my iframe using top.frame.location=url (or do i need to convert this to another variable first?)
Have I missed anything? If not, I'll give it a go now and upload it (my PC is so slow tonight and I've got a damn persistent pop-up that's annoying the !*#$ outta me!)
and a 'c' key that doesn't work properly - so heers!
1. Yes
2. Yes (the second one)
3. This code (#2) will be put on the page that will be displaying the <iframe>
4. Yes, change menu.js. Not sure what you are asking for the second bit...
5. In pages.php, the code in #2 should do all this for you. If you send a query string like this pages.php?url=yourpage.php it will read yourpage.php into the <iframe> if yourpage.php exists, and if it does not, it will print an error message.
tiggerific
06-19-2003, 08:18 AM
Aagh! It's taking me like 90 seonds just to load the Reply sreen!!
Anyway, for item 4 - the url that is being passed is in a subdiretory of the main page (pages.php - whih also displays the iframe). guess i just need to concatenate that (boy that's a hard word with no "c") with the $url right? What is the right way to do that? Is this close?
echo "<iframe name=\"pagetext\" HEIGHT=\"1200\" FRAMEBORDER=\"no\" WIDTH=\"550px\" src=\"webtext\\$url.htm\"></iframe>"
Copied directly so you can check if i've got all the slashes in right (i remembered that from somewhere :) clever ain't i)
promise i will be going away soon :D
lol... :D
Ok, the slashes were a bit off... try this:
echo "<iframe name=\"pagetext\" HEIGHT=\"1200\" FRAMEBORDER=\"no\" WIDTH=\"550\" src=\"webtext/$url.htm\"></iframe>"
if $url equals test, this will load:
webtext/test.htm
tiggerific
06-19-2003, 08:25 AM
if it exists right??
I think you're a genius - each time i replied to you i worked out a little bit more after i hit the reply button - so i think i'm getting the hang of it now.
I'll be running php tutorials for everyone by next week :)
Right, if it exists. I'm assuming that you have you echo statment inside the if statment?
<?PHP
if ($x == 1) { #if $x equals one (ie, if file was found)
echo "<iframe name=\"pagetext\" height=\"1200\" frameborder=\"no\" width=\"550\" src=\"webtext/$url.htm\"></iframe>"; #display the iframe with the file loaded.
}
else if ($x == 2) { #if $x equals two (ie. if file was not foune)
echo "Sorry, that URL is invalid"; #display an error message if the URL is invalid.
}
?>
Let me know when you get your tutorials up... :D
tiggerific
06-19-2003, 08:33 AM
Whaddya think i'm a newbie and don't know what i'm doing??
Of course i got it in the right place :p
tiggerific
06-19-2003, 08:46 AM
Um can i take back that last post?
I uploaded it and everything works perfectly, but...
i don't want the submit form to appear on my page :confused:
i want it to work it out from the address and it didn't.
it displayed the form, and if i type in the url it does display properly, but how can i do it without the text box appearing?
sorry!
Sounds like you are using the first code I posted. You need to be using the second one. The one with the $_GET's instead of the $_POST's
http://forums.webdeveloper.com/showthread.php?s=&postid=60422#post60422
tiggerific
06-19-2003, 08:59 AM
OK - I left the FORM in there (and I went back and copied it again - this damn 'cccc' key! - i forget and use the keyboard shortcut).
Trying to upload again.
Hang on!
tiggerific
06-19-2003, 09:06 AM
Sorry - this is the other half:
<?PHP
$x = 0; #set #x to zero
if (isset($_POST["submit"])) { #if form has been submitted
$url = $_POST["url"]; #set $url to be the value of the input area "url"
$url = "webtext/"&$url&".htm"
$file = @file ($url); #url to open. $file equals the url inputted
if ($file) { #if $file was found
$x = 1; #set $x to one for later use (ie. file was found)
}
else { #if file was not found
$x = 2; #set $x to two for later use (ie. file was not foune)
}
}
?>
The menu correctly sends pages.php?url=mlm
tiggerific
06-19-2003, 09:23 AM
It's after midnight and I'm getting addled again.
Ok.
1. menu.js sends:
'../pages.php?url=mlm
2. pages.php has at the top of the document:
<?PHP
$x = 0; #set #x to zero
if (isset($_POST["submit"])) { #if form has been submitted
$url = $_POST["url"]; #set $url to be the value of the input area "url"
$file = @file ($url); #url to open. $file equals the url inputted
if ($file) { #if $file was found
$x = 1; #set $x to one for later use (ie. file was found)
}
else { #if file was not found
$x = 2; #set $x to two for later use (ie. file was not foune)
}
}
?>
3. pages.php has (in the body):
<?PHP
if ($x == 1) { #if $x equals one (ie, if file was found)
echo "<iframe name=\"pagetext\" HEIGHT=\"1200\" FRAMEBORDER=\"no\" WIDTH=\"550\" src=\"webtext/$url.htm\"></iframe>"; #display the iframe with the file loaded.
}
else if ($x == 2) { #if $x equals two (ie. if file was not foune)
echo "<iframe name=\"pagetext\" HEIGHT=\"1200\" FRAMEBORDER=\"no\" WIDTH=\"550\" src=\"webtext/nofile.htm\"></iframe>"; #display an error message if the URL is invalid.
}
?>
Result - no file displayed in the iframe on pages.htm.
:(
I think I'm going to give up soon.
tiggerific
06-19-2003, 10:32 AM
pyro, pyro, where for art thou pyro?
oh, there you are - sitting at your keyboard shaking your head in amazement as you peruse these extremely insightful posts of mine. I can see you rolling your eyes as well and (what was that you just mumbled under your breath??)
Hmm.
Anyway - I fixed it :)
All of it :)
By myself :)
Without your help :)
Go see ==> www.tigger2.com :)
(choose mlm from the first menu, then anything else cause mlm is the only one that actually exists at the moment)
So now i've got all these wonderful scripts and php and stuff, i spose i better put some content in there :confused:
but I couldn't delete my previous posts to save you the pain of having to wade through all this crap..
bedtime now - 1.30am (you're still my hero!)
Good going... :)
Sorry 'bout not getting back to you... Sometimes real work takes precedence (sometimes... :D)
tiggerific
06-19-2003, 10:43 AM
Not a problem!
Still think you did it on purpose so i'd try and work it out for myself :D
'night pyro - sweet dreams
ps - if you pop downunder (oz) anytime soon, i'll shout ya a bourbon :eek:
Night for you, 10:45 in the morning for me... :D
And yes, I do like it when people actually try to figure things out by themselves rather than wait for me or someone else to just send them code.
Cheers!