1+ <?php
2+ require_once ('db_connect.php ' );
3+
4+ # Find site's root folder, e.g. D:/web/openrails/web
5+ $ root = getenv ("DOCUMENT_ROOT " );
6+ $ path = strtr ($ root , '\\' , '/ ' );
7+
8+ if (!isset ($ _SESSION ['id ' ]) || strlen ($ _SESSION ['id ' ]) < 10 ) {
9+ // Retrieve id from cookie or create a new one.
10+ $ cookie_name = 'or_org3 ' ;
11+ if (isset ($ _COOKIE [$ cookie_name ])) {
12+ $ id = $ _COOKIE [$ cookie_name ];
13+ }else {
14+ $ id = md5 (date ("Y-m-d H:i:s " ) . rand ()); // Uses the MySQL DATETIME format plus random number
15+ setcookie ($ cookie_name , $ id , 2147483647 ); // max time value to expire far into future (19-Jan-2038)
16+ $ sql = "INSERT INTO tVisitor (id) VALUES(' $ id') " ;
17+ if (!mysqli_query ($ dbc , $ sql )) { die ('Error: ' . mysqli_error ($ dbc )); }
18+
19+ $ ip = $ _SERVER ['REMOTE_ADDR ' ]; // Always a real IP address but might be a proxy.
20+ $ sql = "INSERT INTO tVisitor_Attribute (used_by, of_type, of_value) VALUES(' $ id', 'ip', ' $ ip') " ;
21+ if (!mysqli_query ($ dbc , $ sql )) { die ('Error: ' . mysqli_error ($ dbc )); }
22+
23+ if (isset ($ _SERVER ['HTTP_REFERER ' ])){
24+ $ referer = $ _SERVER ['HTTP_REFERER ' ];
25+ }else {
26+ $ referer = 'unknown ' ;
27+ }
28+ $ sql = "INSERT INTO tVisitor_Attribute (used_by, of_type, of_value) VALUES(' $ id', 'referer', ' $ referer') " ;
29+ if (!mysqli_query ($ dbc , $ sql )) { die ('Error: ' . mysqli_error ($ dbc )); }
30+
31+ if (isset ($ _SERVER ['HTTP_USER_AGENT ' ])){
32+ $ agent = $ _SERVER ['HTTP_USER_AGENT ' ];
33+ }else {
34+ $ agent = 'unknown ' ;
35+ }
36+ $ sql = "INSERT INTO tVisitor_Attribute (used_by, of_type, of_value) VALUES(' $ id', 'agent', ' $ agent') " ;
37+ if (!mysqli_query ($ dbc , $ sql )) { die ('Error: ' . mysqli_error ($ dbc )); }
38+ }
39+ // For visitors with cookies turned off, each visit to a webpage results in a new entry and several in Visit and Visitor_Attribute.
40+ // Could change this so each session will be as a different individual.
41+ // Not yet implemented.
42+ // menu.php must be changed to add echo '?', htmlspecialchars(SID); as the first parameter in every relative URL.
43+ // Should be detected automatically by the PHP server, but trials show this isn't the case for v5.2.8 as the setting.
44+ // ini_get("session.use_only_cookies") returns 1 meaning that the SID parameter is ignored by session_start().
45+
46+ // Using Session variables can be dangerous.
47+ // "Please note that if you have register_globals to On, global variables associated to $_SESSION variables are references,
48+ // so this may lead to some weird situations." Re-using $id and changing its value also changes $_SESSION['id']
49+ $ _SESSION ['id ' ] = $ id ;
50+ }
51+
52+
53+ // Record visit made to this webpage
54+ $ folder = strtr (getcwd (), '\\' , '/ ' );
55+ $ made_to = str_replace ($ path , '/ ' , $ folder ); // Remove leading filepath to leave local URL
56+ $ made_to = str_replace ('// ' , '/ ' , $ made_to ); // home page is just '/', but other pages begin with '//' which must become single.
57+
58+ // In a timestamp field, NULL is replaced by current time automatically.
59+ //$sql = "INSERT INTO tVisit (made_by, made_to, visited_on) VALUES('$id', '$made_to', NULL)";
60+ // But not on this server, so ask PHP for the date and time.
61+ $ now = date ("Y-m-d H:i:s " );
62+
63+ /* DISABLED as leading to a 500 Server Error
64+ $sql = "INSERT INTO tVisit (made_by, made_to, visited_on) VALUES('$id', '$made_to', '$now')";
65+ echo "<br>$sql<br>";
66+ if (!mysqli_query($dbc, $sql)) { die('Error: ' . mysqli_error($dbc)); }
67+ */
68+ ?>
0 commit comments