NameSilo

Problem with php visual confirmation generater

Spaceship Spaceship
Watch

Rudy

Established Member
Impact
16
Hey all,
I am trying to get a visual image confirmation script working, to generate images for sign-up confirmation purposes, to help prevent spam bots from registering on a registration form I have.

So here is the code on the registration page, thus far:

Code:
<?PHP
/*
This code is taken from phpBB 2.0.21. It is meant to be a standalone registration.
*/

define('IN_PHPBB', true);
$phpbb_root_path = './forums/';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_PROFILE);
init_userprefs($userdata);
//
// End session management
//

// session id check
if (!empty($HTTP_POST_VARS['sid']) || !empty($HTTP_GET_VARS['sid']))
{
	$sid = (!empty($HTTP_POST_VARS['sid'])) ? $HTTP_POST_VARS['sid'] : $HTTP_GET_VARS['sid'];
}
else
{
	$sid = '';
}

//
// Set default email variables
//
$script_name = preg_replace('/^/?(.*?)/?$/', '1', trim($board_config['script_path']));
$script_name = ( $script_name != '' ) ? $script_name . '/profile.'.$phpEx : 'profile.'.$phpEx;
$server_name = trim($board_config['server_name']);
$server_protocol = ( $board_config['cookie_secure'] ) ? 'https://' : 'http://';
$server_port = ( $board_config['server_port'] <> 80 ) ? ':' . trim($board_config['server_port']) . '/' : '/';

$server_url = $server_protocol . $server_name . $server_port . $script_name;

// -----------------------
// Page specific functions
//
function gen_rand_string($hash)
{
	$rand_str = dss_rand();

	return ( $hash ) ? md5($rand_str) : substr($rand_str, 0, 8);
}
//
// End page specific functions
// ---------------------------

?>
<html>
<head>		
	<title>Students in Action Registration</title>
</head>
<body bgcolor="#FFFFFF">
<form action="forums/profile.php" method="post">
This is a TEST!
<?PHP
// NOTE: if you have CL, you can use the following line:
// require 'includes/init.php';
// otherwise, copy & paste random_str() function from visual.php above.

function random_str ($len)
{
 $ch = "ABCDEFGHJKLMNPQRSTUVWXYZ1234567890";
 $l = strlen ($ch) - 1;
 $str = "";
 for ($i=0; $i < $len; $i++)
 {
	 $x = rand (0, $l);
	 $str .= $ch[$x];
 }
 return strtolower ($str);
}
$ses = random_str(16);
?>

<form method="get" action="register.php">
 <input type="hidden" name="confirm_ses" value="<?php echo $ses;?>">
 Confirmation code: <img src="visual.php?confirm_ses=<?php echo$ses;?>"><br>
 Enter confirmation code: <input type="text" name="confirm_val"><br>
 <input type="submit">
</form>
</form>
</body>
</html>

And here is the code in visual.php:
Code:
<?php
function random_str ($len)
{
 $ch = "ABCDEFGHJKLMNPQRSTUVWXYZ1234567890";
 $l = strlen ($ch) - 1;
 $str = "";
 for ($i=0; $i < $len; $i++)
 {
	 $x = rand (0, $l);
	 $str .= $ch[$x];
 }
 return strtolower ($str);
}


// fill with MySQL configuration
$db_name = 'database';
$db_hostname = 'localhost';
$db_username = 'username';
$db_password = 'password';

if (!$dbh=mysql_pconnect ($db_hostname,$db_username, $db_password))
 { echo mysql_error (); exit; }
mysql_select_db($db_name, $dbh);

// init vars
$confirm_ses = $_GET['confirm_ses'];
$t = random_str (5);
$w = 45;
$h = 21;
$now = mktime ();
$was = mktime () - 600;

mysql_query ("INSERT INTO visual_confirm VALUES ('$confirm_ses', '$t', '$now')");
mysql_query ("DELETE FROM visual_confirm WHERE confirm_time < $was");

header ('(anti-spam-content-type:) image/png');
$gb = imagecreate ($w, $h);
$bgc = imagecolorallocate ($gb, 255, 255, 255);
$fgc = imagecolorallocate ($gb, 0, 0, 0);
$grc = imagecolorallocate ($gb, 200, 200, 200);

imagestring ($gb, 5, 0, 5, $t, $fgc);
imagePNG ($gb);
imagedestroy ($gb);
?>

When I go to view register.php in a web browser, the image is broken. It doesn't show up. So when I click to "view image", I get the following error: The image โ€œhttp://localhost/domain/visual.php?confirm_ses=(randomly generated characters)โ€ cannot be displayed, because it contains errors.

Any idea what's going on, and how I can get this to work?

Thanks,
David
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
try commenting out the following line and you should then see the php generated errors:-

header ('(anti-spam-content-type:) image/png');
 
0
•••
easy tip to save uncommenting code, when you get that error view the image source, errors appear at the top of the page.
 
0
•••
Thanks for the responses.

Interesting, Adam. Thanks for the suggestion. Here's that error:

<b>Fatal error</b>: Call to undefined function imagecreate() in <b>/var/www/studentsinaction/visual.php</b> on line <b>38</b><br />

I'm checking imagecreate() right now to see what it's talking about.... Well, I found what it's talking about, but I have no idea what to do to fix it... obviously the function imagecreate() isn't defined... is that not a default php function? I'm going to google it to see what I can find. In the mean time, any suggestions?

edit
Ok... after a little googling, I found that on some machines & servers, the GD library needs to be enabled:

To make the gd library available for Apache2/mod_php...
Well, I'm running Apache2... more googling. :)

Another Edit
Ok, I'm running Ubuntu, and all of this testing is on my localhost, obviously. I checked my package manager and found that libgd2-noxpm was installed. I removed that, and replaced it with libgd-xpm. Hopefully this will work....

A Final Edit
See the last one (another edit). It didn't work. Any ideas?
 
Last edited:
0
•••
You are probably missing the GD libary. Try to do a
PHP:
<?php
var_dump(gd_info());
?>

If you have GD installed you will get a array with information. The first string should say the version the rest will say what is enabled.

If you get an error you need to install GD. If you own the server and use CPanel just rebuild Apache and make sure GD is selected. If you don't try http://www.boutell.com/gd/ to find a suitable version for your server. I suspect since you use Ubuntu you don't.

As for instructions on installing GD. I know how to do it with Redhat and Fedora and BSD but not Ubuntu so I cannot help you there.

- Steve
 
Last edited:
0
•••
Thanks, Steve. I ran that command (var_dump(gd_info()); ) & it said it was calling an undefined function, so I assume the GD lib isn't installed.

However, in Ubuntu's Package manager, I installed the package "libgd2-xpm", which has the title of GD Graphics Library version 2.
This is the runtime package of the library, built with XPM (X pixmap)
and fontconfig support.

Shouldn't this be it? With the way that Ubuntu works, once it is installed, I shouldn't have to reconfigure apache - all I should now have to do is restart apache2.
 
0
•••
The chances are php is compiled without GD support.

Create a file with the following in it:-

PHP:
<?php
if (in_array('gd', get_loaded_extensions()))
{
	echo 'php is installed with gd support';
}
else 
{
	echo 'gd is NOT enabled in php';
}
?>

name it info.php and place it in he proper place for the webserver and go to it in the browser.

Once open you should see a message telling you wether php was compiled with gd support.

Rudy said:
Shouldn't this be it? With the way that Ubuntu works, once it is installed, I shouldn't have to reconfigure apache - all I should now have to do is restart apache2.

Apache does not have anything to do with GD they are not connected.
 
Last edited:
1
•••
Thanks, Peter.

I saw this original post before you edited it, and searched phpinfo() and didn't find anything with GD support.

When I did what your edited post recommends, sure enough, gd is NOT enabled.... so I guess I need to recompile PHP, or at least edit my php.ini file?

ok, edit:
I found a package for php-gd in the package manager which installs GD support. I installed it... will post back when I know something more. Thanks for the

edit
Yep, it worked. Thanks for the help.
 
Last edited:
0
•••
no problem at all. Yeah I decided to change the post so you didnt have to search through the phpinfo page. Glad the problem is not sorted.
 
0
•••
Happy it is all working now.

- Steve
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back