mysql域名重定向,通过mysql数据库重定向多个网址?

I was wondering if it would be possible to redirect a url to another specific url that is got from a database? For example if www.mydomain.com/page was entered it would redirect to www.mydomain.com/folder/page and if www.mydomain.com/page2 was entered it would redirect to www.mydomain.com/folder/page2.

At the minute I just have redirects in my 404 page with this code:

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$page= 'http://www.mydomain.com/page';

if ($url == $page){

header("HTTP/1.1 301 Moved Permanently");

header( 'Location: http://www.mydomain.com/folder/page' ) ;

}

?>

and I've done this with multiple other pages as well.

Thanks for any help!

解决方案

Sure

you have to first get the url from the database

then redirect it using php's header function

$result = mysql_query('SELECT * FROM `your_table` WHERE `token`= $token');

$row = mysql_fetch_assoc ( $result );

header("Location: page/{$row['url']}");

replace your_table with your table name that contains the redirection rules

however if your redirection rules always consist on adding /page before the url, then check the other answers

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,下面是一个简单的示例,使用MySQL数据库和PHP语言实现在线聊天网页。 首先,创建一个名为`chat`的数据库,并创建一个名为`messages`的表,用于存储聊天消息。表结构如下: ```sql CREATE TABLE messages ( id INT AUTO_INCREMENT PRIMARY KEY, sender VARCHAR(50), message TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ``` 接下来,创建一个`index.php`文件,用于显示聊天界面和处理发送消息的逻辑。 ```php <!DOCTYPE html> <html> <head> <title>在线聊天</title> <style> .chat-box { width: 400px; height: 300px; border: 1px solid #ccc; overflow-y: scroll; } </style> </head> <body> <div class="chat-box" id="chatBox"> <?php // 从数据库中获取聊天消息并显示 $conn = new mysqli('localhost', 'username', 'password', 'chat'); $query = "SELECT * FROM messages ORDER BY created_at ASC"; $result = $conn->query($query); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<p><strong>" . $row['sender'] . ":</strong> " . $row['message'] . "</p>"; } } $conn->close(); ?> </div> <form action="send.php" method="post"> <input type="text" name="sender" placeholder="发送者" required> <br> <textarea name="message" placeholder="消息" required></textarea> <br> <button type="submit">发送</button> </form> <script> // 自动滚动到最新消息 var chatBox = document.getElementById('chatBox'); chatBox.scrollTop = chatBox.scrollHeight; </script> </body> </html> ``` 上面的代码中,使用了一个简单的CSS样式来定义聊天框的外观。通过PHP代码从数据库中获取聊天消息,并将其显示在聊天框中。 然后,创建一个`send.php`文件,用于处理发送消息的逻辑。 ```php <?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $sender = $_POST['sender']; $message = $_POST['message']; // 将消息插入到数据库中 $conn = new mysqli('localhost', 'username', 'password', 'chat'); $query = "INSERT INTO messages (sender, message) VALUES ('$sender', '$message')"; $conn->query($query); $conn->close(); // 重定向回聊天页面 header('Location: index.php'); exit; } ?> ``` 上述代码接收来自表单的发送者和消息内容,并将其插入到数据库中。然后,通过重定向将页面返回到聊天界面。 请注意,在上述代码中,需要将`localhost`、`username`和`password`替换为实际的MySQL数据库连接信息。 这只是一个简单的示例,你可以根据自己的需求对其进行扩展和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值