2017-10-11 59 views
-1

我想用php重定向一個短url到2個url。第一個URL立即重定向,第二個URL在一小時後重定向。當第二個網址在一小時後重定向時,第一個網址停止重定向。一小時後url只重新定向。使用php重定向1個短url到2個url

是否有可能使用PHP?

如果你需要更多的細節問我。謝謝。

更多詳細信息:
我做了一個排序url工具/腳本。我想在一個排序網址上一次添加兩個網址。如:我想在一次google.com and youtube.com短結果一個網址進行排序這兩網址example.com/gdh733

當我訪問短網址sxample.com/gdh733其立即重定向我第一個URL,1小時後第一個URL不是重定向,只有第二個URL youtube.com開始轉向。

希望讓你明白。如果不明白問我。

再次更新:
數據庫:
Database Imagehttp://devlup.com/wp-content/uploads/2010/08/database-structure-php-shortener.png

htaccess的:

RewriteEngine on 
RewriteRule ^([\w\d]{4})$ decoder.php?decode=$1 [L] 

的index.php

<div class="header"> Php URL shortener<hr /></div> 
<div class="content"> 
<form id="form1" name="form1" method="post" action="shorten.php"> 

    <p><strong> Url:</strong> 
    <input type="text" name="url" id="url" size="45" /> 
    </p> 
    <p> 
    <input type="submit" name="Submit" id="Submit" value="Shorten" /> 
    </p> 
    <p>&nbsp;</p> 
</form> 

shorte n.php

<div class="header"> Php URL shortener<hr /></div> 
<div class="content"> 

<?php 

$con = mysql_connect("localhost","masudtoo_short2","masud226688"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
mysql_select_db("masudtoo_short", $con); //Replace with your MySQL DB Name 
$urlinput=mysql_real_escape_string($_POST['url']); 
$id=rand(10000,99999); 
$shorturl=base_convert($id,20,36); 
$sql = "insert into save values('$id','$urlinput','$shorturl')"; 
mysql_query($sql,$con); 
echo "Shortened url is <a href=\"http://short.masudtools.xyz/". $shorturl ."\">http://short.masudtools.xyz/". $shorturl ."</a>"; 
mysql_close($con); 
?> 

</div> 

decoder.php

<?php 

$con = mysql_connect("localhost","masudtoo_short2","masud226688"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
mysql_select_db("masudtoo_short", $con); //Replace with your MySQL DB Name 


$de= mysql_real_escape_string($_GET["decode"]); 



$sql = 'select * from save where shortened="$de"'; 

$result=mysql_query("select * from save where shortened='$de'"); 

while($row = mysql_fetch_array($result)) 
{ 
$res=$row['url']; 
header("location:".$res); 
} 

?> 

這裏我設置2網址和會話PHP代碼?

+1

是的,我們需要更多的細節,也不清楚你在問什麼。 –

+0

是的,這是可能的,如果你需要更多的細節問我_先生,你沒有下訂單。**所有的細節和你已經嘗試應該已經發布的問題。** –

+0

我更新了我的文章@ julien-lachal和saad-suri –

回答

0

當然是的,它是可能的,使用SessionsJavaScript

在這個例子中,我重定向每5秒。 1小時使用:

<?php 
    session_start() ; 

    //If session not set, redirect immediatly. First time only 
    if (!isset($_SESSION["isRedirected"]) || $_SESSION["isRedirected"] != 1) { 
     $_SESSION["isRedirected"] = "1" ; 
     header("Location: index2.php") ; 
    } 

?> 
<html> 
    <head> 

    </head> 
    <body> 
     <h2>Index 1</h2> 

     <script src='https://code.jquery.com/jquery-2.1.4.min.js'></script> 
     <script> 
      //After 5000 milliseconds = 5 seconds, redirect. 
      //for 1 hour use: 1000 * 60 * 60 
      //    1 sec * 60 sec/min * 60 min/hour 
      setTimeout(
       function() { 
        window.location.href = "index2.php" 
       }, 5000 
      ); 
     </script> 
    </body> 
</html> 

創建 「index2.php」 與下面的代碼:1000 * 60 * 60,在代碼

創建 「的index.php」 與下面的代碼說明:

<?php 
    session_start() ; 
?> 
<html> 
    <head> 

    </head> 
    <body> 
     <h2>Index 1</h2> 


     <script src='https://code.jquery.com/jquery-2.1.4.min.js'></script> 
     <script> 
      //After 5000 milliseconds = 5 seconds, redirect. 
      //for 1 hour use: 1000 * 60 * 60 
      //    1 sec * 60 sec/min * 60 min/hour 
      setTimeout(
       function() { 
        window.location.href = "index.php" 
       }, 5000 
      ); 
     </script> 
    </body> 
</html> 

更新代碼,丟棄上面的代碼:

<?php 
    session_start() ; 

    $redirectsArr = array(//Pages to visit, by order 
     "http://google.com", 
     "http://youtube.com" 
    ) ; 

    //If session not set, redirect immediatly. First time only 
    if (!isset($_SESSION["redirectedNdx"])) { 
     $_SESSION["redirectedNdx"] = 0 ; 
     header("Location: " . $redirectsArr[$_SESSION["redirectedNdx"]]) ; 
    } 
    else { //Set the index of the page to visit in the above array 
     if ($_SESSION["redirectedNdx"] == 0) { 
      $_SESSION["redirectedNdx"] = 1 ; 
     } 
     else { 
      $_SESSION["redirectedNdx"] = 0 ; 
     } 
    } 
?> 
<html> 
    <head> 

    </head> 
    <body> 
     <h2>Soon... redirecting to: <?php echo $redirectsArr[$_SESSION["redirectedNdx"]] ?></h2> 

     <script src='https://code.jquery.com/jquery-2.1.4.min.js'></script> 
     <script> 
      //After 5000 milliseconds = 5 seconds, redirect. 
      //for 1 hour use: 1000 * 60 * 60 
      //    1 sec * 60 sec/min * 60 min/hour 
      setTimeout(
       function() { 
        window.location.href = "<?php echo $redirectsArr[$_SESSION["redirectedNdx"]] ?>" 
       }, 5000 
      ); 
     </script> 
    </body> 
</html> 
+0

我更新了我的文章。請再次檢查。 –

+0

檢查更新的代碼! –

+0

你的代碼是好的,但我需要更多的提示。我更新了它。覈實。 –