我想使用php将一个短网址重定向到2个网址。第一个网址立即重定向,第二个网址重定向一小时后。当第二个网址重定向一小时后,第一个网址停止重定向。一小时后,第二个网址只能重定向。
是否可以使用php?
如果您需要更多细节请问我。感谢。
更多详情:
我做了一个排序网址工具/脚本。我想在一个网址上一次添加两个网址。例如:我想立即对这两个网址进行排序google.com and youtube.com
简短结果一个网址example.com/gdh733
当我访问短网址sxample.com/gdh733
时,它立即重定向我第一个网址,1小时后第一个网址没有重定向,只有第二个网址youtube.com
开始重定向。
希望你明白。如果不明白问我。
再次更新:
数据库:
Database Image
或http://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> </p>
</form>
shorten.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个url和session php代码?
答案 0 :(得分:0)
是的,当然可以使用Sessions
和JavaScript
在此示例中,我每5秒重定向一次。
使用1小时:1000 * 60 * 60
,在代码中解释
使用以下代码创建“index.php”:
<?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”:
<?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>