我写了这段代码,将访问者重定向到我的笔记本电脑(动态ip)中托管的服务器
我使用第二个php脚本来更新数据库中的ip
<?php
//redirect.php
$con = mysql_connect($db_hostname,$db_admin,$db_password);
if (!$con){
echo "Couldn connect to server !!";
}
$sql ="select ip from $db_name.redirect WHERE id=0";
$result = mysql_query($sql,$con);
$ip = "error.php";
while($row = mysql_fetch_array($result)){
$ip=$row[0];
}
//Header( "HTTP/1.1 301 Moved Permanently" );
$forward = "Location: http://$ip";
header( $forward ) ;
//Header( "Location: errorMsg.php");
?>
但我的笔记本电脑服务器很少打开,我想知道如果第一次重定向失败,是否可以重定向到errorMsg.php??
jsp也可以实现同样的功能吗??
我想你可以ping IP,如果它没有响应,那么使用errorMsg.php。试试这个:
$dsn = 'mysql:dbname='.$db_name.';host='.$db_hostname;
try
{
$dbh = new PDO($dsn,$db_admin,$db_password);
} catch(PDOEXception $e) {
echo 'Couldn connect to server !!';
}
$sql = 'SELECT ip FROM redirect';
$sth = $dbh->prepare($sql);
$sth->execute();
$ip = $sth->fetchColumn();
$location = 'errorMsg.php';
if($ip && $fp=fsockopen($ip,80,$en,$es,1)) {
fclose($fp);
$location = 'http://'.$ip;
}
header('Location: '.$location);
这假设您有某种方法可以使用所需的IP更新数据库。
附言-我建议PDO,如果它不是显而易见的。