Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: How to call another site from my html form

  1. #1
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to call another site from my html form

    Hi,

    Im new to PHP and I try to call another site from my html form:

    PHP Code:
    <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 Code:
    <?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!
    Last edited by dorello; 04-30-2010 at 08:16 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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 Code:
    <?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 Code:
    <?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.
    Last edited by djr33; 04-26-2010 at 05:22 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!
    Last edited by dorello; 04-30-2010 at 08:17 PM.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!

  6. #6
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Last edited by djr33; 04-26-2010 at 11:44 PM. Reason: fixed code
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you! Ill try it out in a minute.

    I just found this code on the internet, I think it is interesting:

    PHP Code:
    <!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


    Hmmjavascript and php on one page... I tried it outbut it is searching for files and stuff just on your domainwhere it resides - as index.html lets say.

    Is it possible to change itso it would just browse the site entered?

    Thank you so much for your time!!!! 
    Last edited by Snookerman; 05-10-2010 at 09:07 AM. Reason: added [/php] end tag

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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;
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    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!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •