Herself's Webtools

Scripts, HowTos, Templates, Plugins, Widgets, Tips

Archive for August, 2008

How to force an HTTP error code from PHP

without comments

If something happens in your php script you may want to force an HTTP error, rather than continue or redirect the user to another page. This is very simple to do. Just be sure the first line of output from your script to the browser is

header(‘HTTP/1.0 403 Forbidden’);

or

header(‘HTTP/1.0 404 Not found’);

Or which ever standard code other than 200 you wish to send.

If you are using the Security Plugin instead of redirecting the attacker to an error page you can send back an HTTP error.

There are two HTML Error pages in the security script, one for blocked ips, one for all else.


// print error page
print “<html>\n”;
print “<head><title>Banned</title></head>\n”;
print “<body>\n”;
print “<h2>Banned: $blacklisted:  $code</h2>\n”;
print “<p>If you believe this to be in error please contact <a href=\”mailto:timestocome@gmail.com\”>timestocome@gmail.com</a>”;
print “</body>\n”;
print “</html>\n”;


What you will do is remove or comment out the error page and in it’s place put:
header(‘HTTP/1.0 403 Forbidden’);

No print, no extra quotes – use it just as I have it here.


$code = “Sorry but you are listed on our ip blacklist”;
global $wpdb;
header(‘HTTP/1.0 403 Forbidden’);
exit();


Feel free to use any of the standard codes. HTTP/1.1 Error Codes

Written by Linda MacPhee-Cobb

August 29th, 2008 at 11:40 am

Posted in how to,php,security

Use PHP to add your Twitter feed to any page

with 6 comments

While I was building my wonder of me portal page for TimesToCome I thought it’d be neat to add in my twitter stream.

Here is some very simple PHP code to do so. Now I was lazy and I only check for one link in the tweet. 99% of the time that is sufficient. The only thing you need to change is to change the user name from timestocome to your username. Any other changes are optional.

$username = “timestocome”;
$tweets = fetch_rss(‘http://twitter.com/statuses/user_timeline/’.$username.’.rss’);

foreach ( $tweets->items as $tweet ){

print “<hr>”;

$twit = ” “.substr(strstr($tweet['description'],’: ‘), 2, strlen($tweet['description'])).” “;
$link = $tweet['link'];

// hack around php null == 0 ugh!
$xtwit = “x” . $twit;
$mark = strpos( $xtwit, “http://” );

if ( $mark > 0 ){ // we have a link in our tweet perhaps more than one

$l = substr ( $xtwit, $mark, strlen($xtwit) );
$l = substr ( $l, 0, strpos( $l, ” ” ));
$full_link = “<a href=\”$l\”>$l</a>”;
$xtwit = str_replace ( $l, $full_link, $xtwit );
$xtwit = substr ( $xtwit, 1, strlen($xtwit));

print “<br><p>$xtwit <a href=\”$link\”>#</a></p>”;

}else{

print “<br><p>$twit <a href=\”$link\”>#</a></p>”;

}
$time = $tweet['pubdate'];
$time = substr( $time, 0, strlen($time)-6);
print  ” $time “;

}

Written by Linda MacPhee-Cobb

August 18th, 2008 at 5:00 am