January 16, 200917 yr Hi, I have a PHP script written which gathers a little information on the visitor to my site and then puts it into a MYSQL table. Everything is working fin except I use $_SERVER['PHP_SELF']; to see which page they visited but I have variables in the url for example topic.php?idt=16 and the PHP command only inserts /topic.php onto the database. How can I get it to add the variables too? Thank you in advance.
January 16, 200917 yr Hi, I have a PHP script written which gathers a little information on the visitor to my site and then puts it into a MYSQL table. Everything is working fin except I use $_SERVER['PHP_SELF']; to see which page they visited but I have variables in the url for example topic.php?idt=16 and the PHP command only inserts /topic.php onto the database. How can I get it to add the variables too? Thank you in advance. Hello can we see your code please
January 16, 200917 yr Author <? $query = "SELECT COUNT(*) AS numrows FROM topics"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows']; $query = "SELECT COUNT(*) AS numrows FROM comments"; $result = mysql_query($query) or die('Error, query failed'); $rowz = mysql_fetch_array($result, MYSQL_ASSOC); $coms = $rowz['numrows']; $ip = $_SERVER['REMOTE_ADDR']; if ($ip != '82.25.87.36') { $page = $_SERVER['PHP_SELF']; $ref = $HTTP_REFERER; mysql_query("INSERT INTO `hits` ( `id` , `ip`, `page`, `ref`) VALUES (NULL, '$ip', '$page', '$ref')"); } $query = "SELECT COUNT(*) AS numrows FROM hits"; $result = mysql_query($query) or die('Error, query failed'); $row1 = mysql_fetch_array($result, MYSQL_ASSOC); $hits = $row1['numrows']; $query = "SELECT COUNT(DISTINCT ip) AS numrows FROM hits"; $result = mysql_query($query) or die('Error, query failed'); $row2 = mysql_fetch_array($result, MYSQL_ASSOC); $visits = $row2['numrows']; echo "<li>"; echo "Topics: $numrows"; echo "</li>"; echo "<li>"; echo "Comments: $coms"; echo "</li>"; echo "<li>"; echo "Hits: $hits"; echo "</li>"; echo "<li>"; echo "Visitors: $visits"; echo "</li>"; ?>
January 16, 200917 yr Everything is working fin except I use $_SERVER['PHP_SELF']; to see which page they visited but I have variables in the url for example topic.php?idt=16 and the PHP command only inserts /topic.php onto the database. You need to also use $_SERVER['QUERY_STRING'] to get the query string parameters. Note also PHP_SELF is the location of the file on the hard-drive; in the case you map a single PHP script to several URLs and you want the actual URL used to access the page, you want to use REQUEST_URI instead. So overall, something like: $page = $_SERVER['REQUEST_URI'] + "?" + $_SERVER['QUERY_STRING'];
January 17, 200917 yr Author You need to also use $_SERVER['QUERY_STRING'] to get the query string parameters. Note also PHP_SELF is the location of the file on the hard-drive; in the case you map a single PHP script to several URLs and you want the actual URL used to access the page, you want to use REQUEST_URI instead. So overall, something like: $page = $_SERVER['REQUEST_URI'] + "?" + $_SERVER['QUERY_STRING']; I just tried that and the value in the database I get is "0"
January 17, 200917 yr Sorry, force of habit using + as the string concatenation operator. Those should be "." in PHP. After another careful thought, I realised REQUEST_URI also includes the query string, so in fact all you need is: $page = $_SERVER['REQUEST_URI'];
Create an account or sign in to comment