View Full Version : How to call another site from my html form
dorello
04-26-2010, 02:33 PM
Hi,
Im new to PHP and I try to call another site from my html form:
<form name="form1" method="post" action="code.php">
<table border=0>
<tr>
<td>
<font color="#000000" size="-1" face="Verdana,sans-serif"><B>URL:</B></font>
</td>
<td>
</td>
<td>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</form>
and the code.php:
<?php
global $url;
if(isset($_POST['url'])){
$url = $_POST['url'];
$validurl=0;
}
if(checkURL($url)){
$validurl=1;
}else{
}
function checkURL ($url)
{
$url = @parse_url($url);
if (!$url) {
return false;
}
}
echo $url;
?>
Now the problem is, Im getting only the new string on the new page, instead of the page was called.
As I said, Im very new to php, and any help wolud be very appreciated.
Thanks in advance!
djr33
04-26-2010, 05:10 PM
I'm not quite sure what is going on. Do you have a page we could test? In this case at least that would show us what happens with varied input to the form.
There are a few things I notice:
1. Using properly aligned tabs may seem annoying at first, but it makes the whole script a lot easier to read. For example, using }else{ looks confusing to me. I'd put it on the next line just like a new if () {.
2. Functions need to be declared before they can be called. I don't know if this is giving you an error or if for some reason it is working, but placing function definitions above the rest of the code is important and makes organization simpler.
3. Your function checkURL() seems useless-- no offense, but can't you just use if (@parse_url($url))? You have roughly the correct format for a new function, but it doesn't look necessary.
Now on to the problems:
1. parse_url() splits the URL into an array. echo $url will echo a string (a bit of text), NOT an array. An array is a group of strings (or other values). For this, you can use print_r($url). So the last line of your code should be changed.
2. You are setting $url = parse_url($url) inside a function. Inside a function, all variables remain INTERNAL. This is called variable scope. If you'd like to make that the same as the global variable, you can either make it global in scope (put this inside your function, near the top: global $url;) or you can use the & character when calling the function: checkURL(&$url). I believe this will mean "use this variable in the function and keep it linked to the global variable". Of course that only works when a value is passed to a function through the line calling it-- in other cases you'd need to use global. I see that you do have "global" in your script already, but this only works WITHIN a function. You can't make it global before using it, then put it into a function. This must be called within every lower layer of code.
Anyway, here's your code rewritten:
<?php
if(isset($_POST['url'])){
$url = $_POST['url'];
$validurl=0;
}
if(checkURL($url)){
$validurl=1;
}
else{
$validurl=0;
echo "<script>alert('Please enter a full valid URL including http://');</script>";
}
function checkURL ($url)
{
global $url;
$url = @parse_url($url);
if (!$url) {
return false;
}
}
print_r($url);
?>
Or, much simpler:
<?php
$url = (isset($_POST['url']))?$_POST['url']:'';
$validurl=0;
if (@parse_url($url)) {
$url=parse_url($url);
$validurl=1;
}
else {
echo "<script>alert('Please enter a full valid URL including http://');</script>";
}
print_r($url);
?>
There are always a few ways to write code, but as a rule, simpler is better.
Finally, note that parse_url() is not designed to validate a URL. It can give you a hint, but it's not a guarantee. Because of that, you might need to be using another function entirely or write your own. The easiest way to do this is to google "regex php url match" and see what you get. Regular expressions are pattern matching symbols that will see if a certain string fits a certain pattern. It's very hard to write, but luckily for this I expect someone will have posted the code for validating a URL somewhere.
dorello
04-26-2010, 06:35 PM
Gee, I thank you for the very professional reply, Ill have to learn all this stuff.
What I actualy want to achieve is, when you write a page address, lets say google.com, that page is going to be shown in the same or in a extra window (I mean you see the site and not a text).
Right now Im using your second simpler suggestion, but I get:
Array ( [scheme] => http [host] => www.google.com )
instead of the google site itself (what I actualy want).
Thanks so much!
djr33
04-26-2010, 07:33 PM
I don't understand. parse_url() is intended to split it into an array of it's parts. There you have the scheme as HTTP and the host (domain name). That's what you want, no?
So if you just want to get the URL, then you don't really need any of that unless you want to keep it for verification.
Remove $url=parse_url($url);
Then you will have $url at the end and you can do what you'd like with it.
Redirect to the page or you could even do include($url); but that might give strange results (serving google through your site). It also might trigger some negative reactions from sites that see you as acting like a proxy (serving their page on your page).
It might be easier to just echo the value into the URL for an iframe.
dorello
04-26-2010, 08:28 PM
I removed the line $url=parse_url($url);
What do I need to put at the end? Just --- $url; --- in order to redirect to the inputed page ?
Thanks!
dorello
04-26-2010, 08:46 PM
Sorry for the inconvenience, I found this site on the web: http://www.fleiner.com/redirect.html
This is what I want to achieve, if possible.
djr33
04-26-2010, 08:48 PM
Some code that redirects. A header redirect is possible, or you could output some javascript, or a meta redirect tag.
A header redirect is the most seamless, but it also requires that it must be output before any other text on the page. If so, it'll work well.
Don't forget that some browsers refuse redirects, so you want to later output a message or link to the rare user who doesn't get redirected.
A header redirect looks like this:
header("Location: $url");
$url by itself is just text and will do nothing at all.
dorello
04-26-2010, 09:29 PM
Thank you! Ill try it out in a minute.
I just found this code on the internet, I think it is interesting:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>New Window</title>
<script type="text/javascript">
window.onload=function() {
if (document.getElementById) {
document.getElementById('form1').onsubmit=function() {
}
}
}
function getURL(val){
if (val=='') { alert('Please enter a url or file name.'); }
else {
var win=open(val,id,params);
}
return false;
}
Hmm, javascript and php on one page... I tried it out, but it is searching for files and stuff just on your domain, where it resides - as index.html lets say.
Is it possible to change it, so it would just browse the site entered?
Thank you so much for your time!!!!
djr33
04-26-2010, 11:46 PM
Note that my code was wrong above: I forgot a colon in the header. It's now corrected.
There's no PHP in that. You can use that, I think just by removing this line:
val = 'http://www.coolwebmaster.net/'+val;
dorello
04-27-2010, 05:52 PM
Note that my code was wrong above: I forgot a colon in the header. It's now corrected.
Thank you sir, which code do you mean? You sent me more codes. Are you so kind to send it again?
Thanks!!
dorello
04-27-2010, 05:53 PM
Uups, sorry, I got it, it was the last one with the header:)
dorello
04-27-2010, 05:56 PM
djr33, you are really cool!!!!!
dorello
04-27-2010, 06:15 PM
Ok, what I got now:
as index.html
<form name="form1" method="post" action="code.php">
<table border=0>
<tr>
<td>
<font color="#000000" size="-1" face="Verdana,sans-serif"><B>URL:</B></font>
</td>
<td>
</td>
<td>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</form>
---------------------
for the code.php I dont understand, should I use just:
<?php
header("Location: $url");
?>
... or do I have to combine:
header("Location: $url");
with a previous php code you sent me on page one of this thread?
djr33
04-27-2010, 06:21 PM
Right. It needs to be combined. At the very least:
$url = $_GET['url'];
But it should be more complex, checking that 'url' was sent (isset($_GET['url'])) and also that it's a "valid" URL, though I'm not sure on exactly how to go about checking that. what you used before seems like a reasonable way to confirm it's at least similar to a real URL.
dorello
04-27-2010, 09:01 PM
Thanks, Ill give it a try.
Can you put this into a php file?
<script type="text/javascript">
window.onload=function() {
if (document.getElementById) {
document.getElementById('form1').onsubmit=function() {
}
}
}
function getURL(val){
if ((val=='')||(val=='http://www.')) { alert('Please enter a valid URL including http://www.'); }
else {
val = val;
var win=open(val);
}
return false;
}
</script>
Im such a beginner in php:mad:
djr33
04-27-2010, 09:09 PM
PHP generates HTML. In a .php file it IS HTML unless it is inside php code tags <?php ?>.
dorello
04-27-2010, 11:36 PM
If I put the code between <? tags it wont work.
djr33
04-27-2010, 11:48 PM
That code above is not PHP. It's Javascript. You don't need any PHP on the page.
Create a normal HTML page including that Javascript where you'd like, probably in the <head> section.
Then if you need PHP in the page place it where you want it to generate the HTML and inside <?php....?>
In other words:
Don't think of PHP as special. Think of it as an add-on to html. That's all it is.
Write a NORMAL html page. Then add any <?php ?> blocks as you'd like.
Since you are creating a 2-page system, the easiest way to do this is to create two pages: one with the form and another with the javascript redirect.
It is possible to do it all in one page, but this means placing two pages of HTML between if statements and that can get confusing.
I recommend taking a step back and using some tutorials to get an idea of how PHP, at its most basic level, works. Until you understand what PHP is, in its basic nature, you won't be able to do much with it. Once you get that, everything will become clearer on its own.
dorello
04-28-2010, 07:34 AM
I see your point and youre right.
Still, I need to reach this script (as php or as js), from the form on the html page ( with action="code.php" or action="code.js".
With action=code.js" it doesnt work at this stage unfortunately.
If the code is in the html integrated, it works like a charm, but I need to call the script from html, as I said before. The reason is: Ill have 2 scripts called.
If I solved this one, Ill show you what I mean.
dorello
04-30-2010, 08:14 PM
Thanks, it works great now. Both your codes.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.