当前位置: 动力学知识库 > 问答 > 编程问答 >

php - URLs Redirect To Another Page before redirecting the the URL

问题描述:

I have been learning Objective-C for over two years, and I can't code PHP. I just wanted to know how I can redirect the user once he has entered his URL to another page with adverts on it, and then after 5 seconds redirect him to the entered URL. Please can you supply code, as I am not familiar with PHP at all!

网友答案:

John's suggestion sounds like a good one.

As for the code, to redirect in PHP, use:

header('Location: http://www.example.com/');

http://php.net/manual/en/function.header.php

To get the parameter from the URL, you can use PHP's $_GET function:

$url = $_GET['url'];

If your URL is http://www.example.com/?url=google.com, then you will be able to get the "google.com" using the above code.

http://php.net/manual/en/reserved.variables.get.php

To put it together, try:

$url = $_GET['url'];
header('Location: $url');
网友答案:

Redirect him to "http://MyPageWithAdverts.company.com?redirectUrl=enteredUrl". Let the adverts page sleep for five seconds, then redirect to it's "redirectUrl" parameter.

网友答案:

First you should be aware that the user inputs (like user input for URL) should be in a form.

You can redirect the user using the header function. Example:

header('Location: http://mydomain.com/lalala/index.php');

You can also put the URL on a variable like:

$url = 'http://mydomain.com/lalala/index.php';

Then,

header("Location: '".$url."'");

That's it for redirection. For a certain pause of 5 seconds, you can use the sleep function and the like or you can use javascript. For php, here is the sleep function you need:

sleep(5); // where 5 is the number of seconds.

thanks

网友答案:

When they first come in to, say, http://example.com/somepage, show them the ads page and include a refreshing <meta> tag like this:

<meta http-equiv="refresh" content="5;url=http://example.com/somepage?ads=0">

That should send them to http://example.com/somepage?ads=0 after five seconds. You can use a simple ads=0 CGI parameter to tell the server not show the ads or you can do similar things with a flag in a cookie. When /somepage gets hit without the cookie or without ?ads=0 then show them the ads, if the cookie is set or ?ads=0 is there then show them the real stuff.

You can also do it in JavaScript by setting a timer and redirecting by assigning to window.location.

This sort of thing can be defeated pretty easily but it should be sufficient for the casual browser.

网友答案:

fix this code for redirect to another URL

<?php
  $url = $_GET['url'];  //Get URL
  if($url==""){         // Check URL isn't empty
    echo("Not URL");
  }else{
    echo("Goto: ".$url);
    header('Location: '.$url); // goto URL
  }
  sleep(5); // where 5 is the number of seconds.
?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: