Tag Archives: MySQL

404 Logging

I decided to setup my 404 page to log the wrong addresses people are attempting to use on my site to a mysql database. A simple version of the script below, add it to your 404 page and input the right login and table details.

< ?php
mysql_connect("localhost", "user", "") or die("Cannot connect to DB!");
mysql_select_db("site") or die("Cannot select DB!");

$ip = getRealIpAddr();
$address = mysql_real_escape_string($_SERVER['REQUEST_URI']);

$insert = "INSERT INTO `404_log` (`address`, `ip`) VALUES ('$address' , '$ip')";
mysql_query($insert); 

function getRealIpAddr(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
    $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
?>

getRealIpAddr function from here

Some SQL to setup the table.

CREATE TABLE IF NOT EXISTS `404_log` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `address` varchar(500) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `ip` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Remove greater than symbol from beginning of imported blogger titles and posts in wordpress

For some strange reason whenever I import blogger posts into wordpress a greater than symbol is added at the front of each post title and main text. I looked into the wordpress mySQL database and found that in the table wp_posts the columbs post_title and post_content had the error.

So I spent a while and eventually came up with two queries that removes all greater than arrows from the begining of each post and title:

UPDATE wp_posts SET post_title = TRIM(LEADING '>' FROM post_title)

UPDATE wp_posts SET post_content = TRIM(LEADING '>' FROM post_content)

And now that clears up my problem, although it would be nice to automate this.

I hope this is helpfull to someone.