Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with $_SERVER['PHP_SELF'];

Featured Replies

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.

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

  • 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>";

?>

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'];

  • 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"

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.