Page 2 of 2

Re: Random images in PHP?

Posted: Thu Jun 12, 2008 3:59 am
by kdidymus
Hmm.

I love the code and it works fine at http://www.didymus.org.uk/test.php

However, when I try to integrate the code in to my existing page I get the following errror:
Warning: Cannot modify header information - headers already sent by (output started at /home/kdidymus/public_html/test2.php:24) in /home/kdidymus/public_html/test2.php on line 186
This can be seen at http://www.didymus.org.uk/test2.php

The code for test2.php is included below. Any idea what's going wrong?

Code: Select all

<?php
/* Program: test2.php
 * Desc:    Provides an anti-spam security check.
*/
echo "
<html>
<head>
<title>STEP 1 of 2</title>
</head>
<body>
<div>
<table border='0' width='450' height='340' cellspacing='0' cellpadding='0' id='table1' background='tree/graphics/email.gif'>
<tr>
<td align='center' valign='top'> <form style='margin-bottom:0' name='spam' action='email2.php' method='get' target='main'>
<div align='left'>
<table border='0' width='100%' cellspacing='0' cellpadding='0' id='table2'>
<tr>
<td align='center' valign='top'><b><font face='Arial' size='5' color='#C0C0C0'>STEP 1 of 2</font></b>
<br><font face='Arial' size='2'>(ANTI-SPAM CHECK)</font>
<hr>
<font face='Arial' size='1'>Firstly let's check that you're a genuine visitor and not a 'spambot' looking to harvest our contact details and inundate us with unsolicited e-mails!</font>
<hr>
</td>
</tr><tr><td align='center' valign='top'>";
function assign_rand_value($num)
{
    // accepts 1 - 31
    switch ($num) {
        case "1":
            $rand_value = "a";
            break;
        case "2":
            $rand_value = "b";
            break;
        case "3":
            $rand_value = "c";
            break;
        case "4":
            $rand_value = "d";
            break;
        case "5":
            $rand_value = "e";
            break;
        case "6":
            $rand_value = "f";
            break;
        case "7":
            $rand_value = "g";
            break;
        case "8":
            $rand_value = "h";
            break;
        case "9":
            $rand_value = "j";
            break;
        case "10":
            $rand_value = "k";
            break;
        case "11":
            $rand_value = "m";
            break;
        case "12":
            $rand_value = "n";
            break;
        case "13":
            $rand_value = "p";
            break;
        case "14":
            $rand_value = "q";
            break;
        case "15":
            $rand_value = "r";
            break;
        case "16":
            $rand_value = "s";
            break;
        case "17":
            $rand_value = "t";
            break;
        case "18":
            $rand_value = "u";
            break;
        case "19":
            $rand_value = "v";
            break;
        case "20":
            $rand_value = "w";
            break;
        case "21":
            $rand_value = "x";
            break;
        case "22":
            $rand_value = "y";
            break;
        case "23":
            $rand_value = "z";
            break;
        case "24":
            $rand_value = "2";
            break;
        case "25":
            $rand_value = "3";
            break;
        case "26":
            $rand_value = "4";
            break;
        case "27":
            $rand_value = "5";
            break;
        case "28":
            $rand_value = "6";
            break;
        case "29":
            $rand_value = "7";
            break;
        case "30":
            $rand_value = "8";
            break;
        case "31":
            $rand_value = "9";
            break;
    }
    return $rand_value;
}
 
function get_rand_id($length)
{
    if ($length > 0) {
        $rand_id = "";
        for ($i = 1; $i <= $length; $i++) {
            mt_srand((double)microtime() * 1000000);
            $num = mt_rand(1, 31);
            $rand_id .= assign_rand_value($num);
        }
    }
    return $rand_id;
}
 
$code = get_rand_id(6);
 
$_SESSION['code'] = $code;
 
 
$width = 129;
$height = 34;
$image = imagecreatetruecolor(129, 34);
$grey = imagecolorallocate($image, 180, 180, 180);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$red = imagecolorallocate($image, 200, 0, 0);
$noise = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), mt_rand(100, 200));
$color2 = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), rand(100,
    200));
$color3 = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 100), mt_rand(100,
    200));
$color4 = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), rand(0,
    200));
 
Imagefill($image, 0, 0, $grey);
 
imagesetthickness($image, 1);
 
for ($i = 0; $i < 800; $i++) {
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color2);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $noise);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color3);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $noise);
 
    imageellipse($image, $width / 2, $height / 2, $width, $height, $color4);
}
 
 
imagettftext($image, 20, 2, 26, 25, $black, "Font-On-A-Stick.ttf", $code);
imagettftext($image, 20, 2, 28, 27, $white,
    "Font-On-A-Stick.ttf", $code);
 
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
echo "</td>
</tr>
<tr>
<td align='center' valign='top'>
<br><font face='Arial' size='1'>ENTER CODE SHOWN ABOVE:</font></td>
</tr>
<tr><td align='center' valign='top'><input type='text' name='usercode' size='15' style='background-color:transparent'></td></tr>
<tr><td align='center' valign='top'><input type='submit' value='NEXT>'>
<br>
<font face='Arial' size='1' color='#FF0000'><i><b>NOTE:</b> This is <b><u>not</b></u> case sensitive. Just enter what you see on the screen.</i><br><br></td></tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>";
?>

Re: Random images in PHP?

Posted: Thu Jun 12, 2008 5:14 am
by superdezign
kdidymus wrote:Warning: Cannot modify header information - headers already sent by (output started at /home/kdidymus/public_html/test2.php:24) in /home/kdidymus/public_html/test2.php on line 186
The code attempts to modify the headers after output has already been made. Headers can only be created and modified prior to any output being made to the screen.

Re: Random images in PHP?

Posted: Thu Jun 12, 2008 6:46 am
by kdidymus
That's what I was afraid of.

Obviously I want to use the randomly generated .png image as part of a nicely designed page. But, obviously, this means echoing HTML statements PRIOR to the image being placed.

Anybody know of a good way of working around this issue?

KD.

Re: Random images in PHP?

Posted: Thu Jun 12, 2008 6:56 am
by kdidymus
Okay.

So I've been "playing" with this. This is the code as it stands now:

Code: Select all

<?php
/* Program: email1.php
 * Desc:    Provides an anti-spam security check.
*/
function assign_rand_value($num)
{
    // accepts 1 - 31
    switch ($num) {
        case "1":
            $rand_value = "a";
            break;
        case "2":
            $rand_value = "b";
            break;
        case "3":
            $rand_value = "c";
            break;
        case "4":
            $rand_value = "d";
            break;
        case "5":
            $rand_value = "e";
            break;
        case "6":
            $rand_value = "f";
            break;
        case "7":
            $rand_value = "g";
            break;
        case "8":
            $rand_value = "h";
            break;
        case "9":
            $rand_value = "j";
            break;
        case "10":
            $rand_value = "k";
            break;
        case "11":
            $rand_value = "m";
            break;
        case "12":
            $rand_value = "n";
            break;
        case "13":
            $rand_value = "p";
            break;
        case "14":
            $rand_value = "q";
            break;
        case "15":
            $rand_value = "r";
            break;
        case "16":
            $rand_value = "s";
            break;
        case "17":
            $rand_value = "t";
            break;
        case "18":
            $rand_value = "u";
            break;
        case "19":
            $rand_value = "v";
            break;
        case "20":
            $rand_value = "w";
            break;
        case "21":
            $rand_value = "x";
            break;
        case "22":
            $rand_value = "y";
            break;
        case "23":
            $rand_value = "z";
            break;
        case "24":
            $rand_value = "2";
            break;
        case "25":
            $rand_value = "3";
            break;
        case "26":
            $rand_value = "4";
            break;
        case "27":
            $rand_value = "5";
            break;
        case "28":
            $rand_value = "6";
            break;
        case "29":
            $rand_value = "7";
            break;
        case "30":
            $rand_value = "8";
            break;
        case "31":
            $rand_value = "9";
            break;
    }
    return $rand_value;
}
 
function get_rand_id($length)
{
    if ($length > 0) {
        $rand_id = "";
        for ($i = 1; $i <= $length; $i++) {
            mt_srand((double)microtime() * 1000000);
            $num = mt_rand(1, 31);
            $rand_id .= assign_rand_value($num);
        }
    }
    return $rand_id;
}
 
$code = get_rand_id(6);
 
$_SESSION['code'] = $code;
 
 
$width = 129;
$height = 34;
$image = imagecreatetruecolor(129, 34);
$grey = imagecolorallocate($image, 180, 180, 180);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$red = imagecolorallocate($image, 200, 0, 0);
$noise = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), mt_rand(100, 200));
$color2 = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), rand(100,
    200));
$color3 = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 100), mt_rand(100,
    200));
$color4 = imagecolorallocate($image, rand(0, 255), mt_rand(0, 100), rand(0,
    200));
 
Imagefill($image, 0, 0, $grey);
 
imagesetthickness($image, 1);
 
for ($i = 0; $i < 800; $i++) {
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color2);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $noise);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $color3);
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width),
        mt_rand(0, $height), $noise);
 
    imageellipse($image, $width / 2, $height / 2, $width, $height, $color4);
}
 
 
imagettftext($image, 20, 2, 26, 25, $black, "Font-On-A-Stick.ttf", $code);
imagettftext($image, 20, 2, 28, 27, $white,
    "Font-On-A-Stick.ttf", $code);
 
header("Content-Type: image/png");
echo "
<html>
<head>
<title>STEP 2 of 2</title>
</head>
<body>
<div>
<table border='0' width='450' height='340' cellspacing='0' cellpadding='0' id='table1' background='tree/graphics/email.gif'>
<tr>
<td align='center' valign='top'> <form style='margin-bottom:0' name='spam' action='email2.php' method='get' target='main'>
<div align='left'>
<table border='0' width='100%' cellspacing='0' cellpadding='0' id='table2'>
<tr>
<td align='center' valign='top'><b><font face='Arial' size='5' color='#C0C0C0'>STEP 1 of 2</font></b>
<br><font face='Arial' size='2'>(ANTI-SPAM CHECK)</font>
<hr>
<font face='Arial' size='1'>Firstly let's check that you're a genuine visitor and not a 'spambot' looking to harvest our contact details and inundate us with unsolicited e-mails!</font>
<hr>
</td>
</tr><tr><td align='center' valign='top'>";
echo "imagepng($image)";
imagedestroy($image);
echo "</td>
</tr>
<tr>
<td align='center' valign='top'>
<br><font face='Arial' size='1'>ENTER CODE SHOWN ABOVE:</font></td>
</tr>
<tr><td align='center' valign='top'><input type='text' name='usercode' size='15' style='background-color:transparent'></td></tr>
<tr><td align='center' valign='top'><input type='submit' value='NEXT>'>
<br>
<font face='Arial' size='1' color='#FF0000'><i><b>NOTE:</b> This is <b><u>not</b></u> case sensitive. Just enter what you see on the screen.</i><br><br></td></tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>";
?>
It works much better now apart from no image is shown. Simply the words:
imagepng(Resource id #1)
Take a look at http://www.didymus.org.uk/test3.php

I know that this is because the header statement is made at the top of the page before I start to echo the HTML.

Help. Please. Aargh!!!

KD.

AJAX

Posted: Thu Jun 12, 2008 7:04 am
by iwtest
JavaScript

Code: Select all

 
//  JavaScript Document
// XMLHttpRequest object created after checking wheather the browser are compatible with ajax.
 
function GetXmlHttpObject()
{
 
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}
 
//Getting XMLHttpRequest object for starting Ajax functionalities.
 
function select_sort_by(state)
{
 //alert(state);
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
 {
    alert ("Your browser does not support AJAX!");
    return;
 }
 else
 {
  if(state)
  {
      xmlHttp.onreadystatechange = stateChanged12;
      url = "ajax-php/feature_sortby.php";
      url = url + "?sort_id=" + state + "&u=" + Math.random(); 
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
  }
  
 }   
}
 
//After getting response from server,response text is being manupulated here.
 
function stateChanged12()
{
     if (xmlHttp.readyState == 4)
     {
        var str = xmlHttp.responseText;
        window.location.href = str;
     }
}
 
Ajax PHP

Code: Select all

 
<?php
 
$visitor_id = $_SESSION['_UID'];
$sort_by = $_REQUEST['sort_id'];
        
        // SQL :: UPDATE VIEWER_SETTINGS TABLE //
        $query = "UPDATE `$table_viewer_settings` SET `feature_sortby` = '$sort_by' WHERE `visitor_id` = '$visitor_id'";
        $res = mysql_query($query);
        
        echo “successfully update”;
?>
 
----------- What ever u want to in php can do just echo msg; which javascript in last function. In javascript have 3 function, first one for get xml object (no changes done – copy as it is) , Second one for get value from some html pages value from some field , and just send it to php file to check or update whatever it may be. Then just echo the msg, and this msg catch third functions and replace the specific html file field changes…
Use It……….. Best of Luck

Re: Random images in PHP?

Posted: Thu Jun 12, 2008 7:11 am
by superdezign
kdidymus wrote:It works much better now apart from no image is shown. Simply the words:
imagepng(Resource id #1)
Thats because you echo those words.

Code: Select all

echo "imagepng($image)";
Something look wrong there?

You can only display a file on an HTML page. In other words, the header does not belong on this page, but rather in a separate file which will act as the image. Headers are a part of the page, and and image header is a part of an image. Then, you'd use that file.

i.e.

Code: Select all

<img src="http://www.website.tld/captcha.php" alt="Captcha Image" />