简体   繁体   English

如何使用php将请求的URL保存到数据库中的apache?

[英]How to save the url of requests to an apache in to database with php?

I wanted to save the requested urls of my website in to a table in database. 我想将网站的请求网址保存到数据库的表中。

For example if user1 requests the url myserver.com/images/header.png , the below record should be saved in to database. 例如,如果user1请求url myserver.com/images/header.png ,则以下记录应保存到数据库中。

id    user    requested_url
1     user1   /images/header.png

EDIT: My main problem is how to know the requested Urls and pass them to a php file! 编辑:我的主要问题是如何知道请求的Urls并将它们传递给php文件!

Have a look at Apaches' mod_log_mysql or mod_log_sql . 看看mod_log_mysqlmod_log_sql They will allow you to forward apache logs to SQL servers. 它们将允许您将apache日志转发到SQL服务器。

Readup: 电文读出:

http://onlamp.com/pub/a/apache/2005/02/10/database_logs.html?page=2 http://onlamp.com/pub/a/apache/2005/02/10/database_logs.html?page=2

You would have to pass everything via a PHP file that has access to your user session. 您将必须通过可访问用户会话的PHP文件传递所有内容。

First thing you need to do is setup a .htaccess file to redirect everything to a specific PHP file. 您需要做的第一件事是设置一个.htaccess文件,以将所有内容重定向到特定的PHP文件。

Then program to pass the requested file to the user and also log the request. 然后编程以将请求的文件传递给用户并记录请求。

Make sure when you do this you exclude the files that are accessed by regular site pages so the script still works as usual just the non page elements like images css js pass through this file. 确保在执行此操作时,您排除了常规站点页面访问的文件,因此脚本仍然照常运行,只是非页面元素(如图像css js)通过此文件。

Add to one of your main script files the same code that logs request to the pages. 将与请求记录到页面相同的代码添加到您的主脚本文件之一。

You should use a DBMS and query to it in order to save the data in your desired table. 您应该使用DBMS并对其进行查询,以便将数据保存在所需的表中。 In MySQL it would be: 在MySQL中,它将是:

INSERT INTO tablename VALUES ( NULL , 'user1', '/images/header.png');

Edit, since someone gave me a -1. 编辑,因为有人给了我-1。 You should add this query-call in your index.php or whatever FrontPage Controller-page you are using to handle all your pages. 您应该在index.php或用于处理所有页面的任何FrontPage Controller-page中添加此查询调用。 Using htaccess you can also log 'hotlinked' files. 使用htaccess,您还可以记录“热链接”文件。

You should connect to the database first and then insert, here is a an example using PDO: 您应该先连接到数据库,然后再插入,这是使用PDO的示例:

$dsn = 'mysql:host=localhost;dbname=testdb'; // mysql
//$dsn = 'mssql:host=localhost;dbname=testdb'; // mssql
$db = new PDO($dsn, 'username', 'password', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

$stmt = $db->prepare("INSERT INTO `table` VALUES ( 0 , :user, :page)");
$stmt->execute(array(':user' => $username, ':page' => $_SERVER['REQUEST_URI']));

I'm guessing first field should be ID and auto increment. 我猜第一个字段应该是ID和自动递增。

The connection and insert must be in every page, maybe a global file. 连接和插入必须在每个页面中,也许是一个全局文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM