title: Save IP and other visitor information in database using PHP
body: <p>I am trying to store visitor information in database. I need to create a table if it doesn't exist, and then store information. But the information is not getting stored and I need help with the code.
  Code --</p>
  
  <pre><code>// visitor information
  $ip = addslashes((getenv(HTTP_X_FORWARDED_FOR)) ? getenv(HTTP_X_FORWARDED_FOR) : getenv(REMOTE_ADDR));
  $visitorReferrer = addslashes(( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : 'Referrer undetectable.');
  $visitorBrowser = addslashes(( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : 'Browser undetectable.');
  
  // connect database
  $con = mysql_connect("localhost","UNAME","PASS");
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }
  
  // Create table
  mysql_select_db("DBNAME", $con);
  $sql = "CREATE TABLE IF NOT EXISTS `logs` (
   `id` int(11) unsigned NOT NULL auto_increment,
   `ip` varchar(255) NOT NULL default '',
   `brow` varchar(255) NOT NULL default '',
   `times` varchar(500) NOT NULL default '',
    PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
  
  // query
  mysql_query($sql,$con);
  mysql_close($con);
  
  mysql_query("INSERT INTO 'logs' (ip, ref, brow, times ) VALUES('{$ip}', '{$visitorReferrer}', '{$visitorBrowser}', now())");
  mysql_close($con);
  
  // image header to browser.
  header("Content-Type: image/jpeg");
  
  // create image and allocate colors...
  $im = @imagecreate(110, 20) or die("Cannot Initialize new GD image stream");
  $background_color = imagecolorallocate($im, 0, 0, 0);
  $text_color = imagecolorallocate($im, 233, 14, 91);
  
  // IP text
  imagestring($im, 1, 5, 5,  "Hello $ip !", $text_color);
  imagepng($im);
  imagedestroy($im);
  </code></pre>
  
  <p>Still not working with the suggestion, (screenshot here)
  <a href="http://0dae.in/cap1.png" rel="nofollow">http://0dae.in/cap1.png</a> "phpadmin"</p>