JavaScript redirect

by
Amir Khan
Profile picture of Amir Khan
Posts: Threads: Thanks: Account age: less than a second
7 replies
So I want to send people to a page that will eventualy redirect to them to the content they want, I am having a problem because i want the code to take a variable from the url, for example the site I want it to redirext to would be specified in the original url, for example:

PHP Code:
www.site.com/file.php?page=www.redirectedsite.com 
Redirected site being the new url...

At first i tried this, combining some php

PHP Code:
<html>
<head>
<script type="text/javascript">
function delayedRedirect(){
    window.location = "<?php echo $_GET['page']; ?>"
}
</script>
</head>
<body onLoad="setTimeout('delayedRedirect()', 3000)">
<h2>You'll be redirected soon!</h2>
</body>
</html>
If I replace the red bit with a normal url such as Google It will redirect no problem but with the php It takes the url but gives me www.site.com/http://www.google.com

I have tried to explain the best I can, any help would be wonderful

Thanks In advance!

I have also tried this but it dosent even pick up the redirect url...

PHP Code:
<html>
<
head>
<
script type="text/javascript">
var 
link getUrlVars()["page"];
function 
delayedRedirect(){
    
window.location link
}
</script>
</head>
<body onLoad="setTimeout('delayedRedirect()', 3000)">
<h2>You'll be redirected soon!</h2>
</body>
</html> 
#javascript #redirect
  • Profile picture of the author Amir Khan
    Amir Khan
    Profile picture of Amir Khan
    Posts: Threads: Thanks: Account age: less than a second
    By "red" bit I meant the entire PHP statment, I cant seem to edit my above post :S
  • Profile picture of the author khtm
    khtm
    Profile picture of khtm
    Posts: Threads: Thanks: Account age: less than a second
    You don't need to use Javascript to do the redirect. Or is it just that you wanted the timer function to work as well?

    Just do it all with PHP (note that I prefixed the location with http:// since your example only had www.)

    <?php

    $page = $_GET['page'];
    header("Location: http://".$page);

    ?>


    However, if you must use JS, you're super close! It's probably that you're just too close to the problem to see the solution

    Again, it's that a proper URL is not just www, you need the http://. That's why the JS code thinks the new location is an internal page, not a new domain.

    <html>
    <head>
    <script type="text/javascript">
    function delayedRedirect(){
    window.location = "<?php echo "http://".$_GET['page']; ?>"
    }
    </script>
    </head>
    <body onLoad="setTimeout('delayedRedirect()', 3000)">
    <h2>You'll be redirected soon!</h2>
    </body>
    </html>


    You could also use the PHP parse_url() function to check if the "page" var is a real URL.
    http://php.net/manual/en/function.parse-url.php
    • Profile picture of the author Amir Khan
      Amir Khan
      Profile picture of Amir Khan
      Posts: Threads: Thanks: Account age: less than a second
      Thankyou, dam I was close, I had tried HTTP$_Get. anyway thankyou for the help, I need javascript for the delayed redirect
      • Profile picture of the author saschakimmel
        saschakimmel
        Profile picture of saschakimmel
        Posts: Threads: Thanks: Account age: less than a second
        What about this one?
        <html>
        <
        head>
        <
        script type="text/javascript">
        function getUrlParam(name)
        {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results != null) {
        return results[1];
        }
        }
        function
        delayedRedirect(){
        window.location = 'http://'+getUrlParam('page');
        }
        </script>
        </head>
        <body onLoad="setTimeout('delayedRedirect()', 3000)">
        <h2>You'll be redirected soon!</h2>
        </body>
        </html>
        Signature

        ** Get my ViralListMachine software now for free and build your own list virally by giving away free stuff @ http://www.virallistmachinegiveaway.com **

        • Profile picture of the author Amir Khan
          Amir Khan
          Profile picture of Amir Khan
          Posts: Threads: Thanks: Account age: less than a second
          Originally Posted by saschakimmel View Post

          What about this one?
          <html>
          <
          head>
          <
          script type="text/javascript">
          function getUrlParam(name)
          {
          name = name.replace(/[[]/,"\[").replace(/[]]/,"\]");
          var regexS = "[\?&]"+name+"=([^&#]*)";
          var regex = new RegExp(regexS);
          var results = regex.exec(window.location.href);
          if (results != null) {
          return results[1];
          }
          }
          function
          delayedRedirect(){
          window.location = 'http://'+getUrlParam('page');
          }
          </script>
          </head>
          <body onLoad="setTimeout('delayedRedirect()', 3000)">
          <h2>You'll be redirected soon!</h2>
          </body>
          </html>
          I prefer my one, as I understand it, I gave yours a look for a good 10 minutes an concluded its to complex for me.

          Thanks anyway I have saved a copy
          • Profile picture of the author saschakimmel
            saschakimmel
            Profile picture of saschakimmel
            Posts: Threads: Thanks: Account age: less than a second
            Sorry, but this is completely unsafe!
            window.location = "<?php echo $_GET['page']; ?>"

            You should never ever do that!
            The only thing that "my" code does it read the
            domain name from the URL via JavaScript and make sure
            your code stays secure and it not unsecure!

            Just try this:
            http://www.example.com/
            file.php?page=";</script><h1>Unsafe!</h1><script>document.write("

            You can insert arbitrary code on your website by using the insecure version!

            Signature

            ** Get my ViralListMachine software now for free and build your own list virally by giving away free stuff @ http://www.virallistmachinegiveaway.com **

  • Profile picture of the author Amir Khan
    Amir Khan
    Profile picture of Amir Khan
    Posts: Threads: Thanks: Account age: less than a second
    Ah forgot about that slight issue..

Trending Topics