Herself's Webtools

Scripts, HowTos, Templates, Plugins, Widgets, Tips

Archive for the ‘how to’ Category

How to add photos from Coppermine to any PHP page

without comments

Some asked how to add Coppermine photos to WordPress pages. This code will work for any page.

You need to replace MYSQL_SERVER with your database server, localhost usually works just fine.
USER_NAME is the user name for your Coppermine database
PASSWORD is the password for your Coppermine database
DATABASE_NAME is the name of your Coppermine database

Also replace YOUR_DOMAIN with the domain your Coppermine albums are hosted on. You might also need to change the path.

This code will give you a horizontal table of 3 thumbnail photos.


If you are using WordPress, you need to download any page from your theme. Change the file name to Coppermine and the Template name to Coppermine.

Remove the stuff in the middle. On my template that is everything between<div id=”content” class=”page”> and </div> but your template might be different.  The past this code into that section being sure to add your password, database etc.

When you are done, upload the new template to your theme directory.

Create a new page and choose Coppermine as the template.  All the work is done.  You can add text or anything else to the page.


<?php
if ( !($coppermine_db = mysql_connect( “MYSQL_SERVER”, “USER_NAME”, “PASSWORD” ))){
die ( “Can not connect to server” );
}else{
//select db
if ( !(mysql_select_db(“DATABASE_NAME”, $coppermine_db ))){
die ( “Can not select database” );
}
}

//ask mysql db for the path and file name of last five images uploaded
$coppermine_query = “select filepath, filename, ctime from cpg_pictures order by ctime desc limit 3;”;

$coppermine_result = mysql_query($coppermine_query);
$count = mysql_numrows( $coppermine_result );

//start link
$link =  “<center>”;

$link .= “<table border=3>”;
$link .=  “<th colspan=3><a href=\”http://YOUR_DOMAIN.com/coppermine/\”>Recent Photos</th><tr>”;
$i = 0;
while ( $i < $count ){

$path = mysql_result($coppermine_result, $i, “filepath” );
$name = mysql_result($coppermine_result, $i, “filename” );

$link .= “<td><img src=\”http://YOUR_DOMAIN.com/coppermine/albums/$path” . “thumb_$name\”></td>”;
$i++;
}

$link .= “</tr></table>”;
$link .= “</center>”;

//end link

//clean up
mysql_close();

print $link;

You can see an example of this code TimesToCome where I pull 3 photos from 3 different Coppermine albums onto the page.

Written by Linda MacPhee-Cobb

September 22nd, 2008 at 4:24 pm

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