简体   繁体   中英

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.

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!

Have a look at Apaches' mod_log_mysql or mod_log_sql . They will allow you to forward apache logs to SQL servers.

Readup:

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.

First thing you need to do is setup a .htaccess file to redirect everything to a specific PHP file.

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.

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. In MySQL it would be:

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

Edit, since someone gave me a -1. You should add this query-call in your index.php or whatever FrontPage Controller-page you are using to handle all your pages. Using htaccess you can also log 'hotlinked' files.

You should connect to the database first and then insert, here is a an example using 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.

The connection and insert must be in every page, maybe a global file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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