Php-mysql Course

It is simple to get the visitor's IP in PHP, just using the $_SERVER['REMOTE_ADDR'] variable.
- Example:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo 'Your IP is: '. $ip;
?>
But this method is not 100% sure. There are some servers that return the server IP when using $_SERVER['REMOTE_ADDR']. Not know why, maybe because they are behind of a proxy, or some server setings.

Generally, to get the user IP, on most servers it can be used the variable presented above, but if returns the IP of the server, there is also another way, the function presented below.
This function returns the visitor IP (even if the $_SERVER['REMOTE_ADDR'] contains the server IP) because it gets the IP from $_SERVER['HTTP_X_FORWARDED_FOR'].
Just add it in your script, and call it when you need (like in the example below).
// Function to get the user /visitor IP in PHP ( https://coursesweb.net/ )
function getUserIP() {
  $ip = $_SERVER['REMOTE_ADDR'];
  if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
    $ip = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
  }
  return $ip;
}
- Example:
<?php
// Function to get the user /visitor IP in PHP ( https://coursesweb.net/ )
function getUserIP() {
  $ip = $_SERVER['REMOTE_ADDR'];
  if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
    $ip = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
  }
  return $ip;
}

// Cals the getUserIP() function, and output the IP
$ip = getUserIP();
echo $ip;
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag can be used to create input text field in web page?
<form> <input> <div>
<input type="text" name="a_name" value="val" />
Which CSS property displays the text in a small-caps font?
display font-variant font-style
h3 {
  font-variant: small-caps;
}
What instruction displays a notice box with a message inside it, in JavaScript?
for() Date() alert()
var msg = "Visit CoursesWeb.net";
alert(msg);
Indicate the PHP code used to get the users IP.
$_SERVER["HTTP_USER_AGENT"] $_SERVER["REMOTE_ADDR"] $_GET[]
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
Get visitor IP in PHP

Last accessed pages

  1. Star shapes with CSS (10465)
  2. setTimeout and this with bind() method in JavaScript class (4304)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (67646)
  4. Clear Canvas Context (7716)
  5. PHP Predefined Constants (580)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (807)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (565)
  3. SHA1 Encrypt data in JavaScript (433)
  4. Create simple Website with PHP (398)
  5. Read Excel file data in PHP - PhpExcelReader (392)