Jump to content

Need PHP guru to fix advanced preg_replace errors


golfwebuk

Recommended Posts

Okay here goes...

 

I have created an application which grabs HTML templates from a MySQL database.  It then parses the HTML templates and any PHP variables stored within them.  Here is an example of how I have stored HTML/PHP variables in the database;

 

<html>{$dtsettings[showSecurityImage]}</html>

 

The above code contains html and a PHP variable which is converted into a PHP function and executed once parsed.  Here is the code which parses the HTML template:

 

$template = preg_replace('/\{\$dtsettings\[(.*?)\]\}/e', 'eval($this->dtsettings["$1"])', $template);

 

The above code extracts a function called ShowSecurityImage() which outputs a CAPTCHA image.  The function is shown below;

 

function ShowSecurityImage() {

	// make a string with all the characters that we 
	// want to use as the verification code
	$alphanum  = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPRSTUVWXYZ23456789";

	// generate the verication code 
	$rand = substr(str_shuffle($alphanum), 0, 6);

	// create an image object using the chosen background
	$image = imagecreatefromjpeg(DIR.'/images/verification/background1.jpg');

	$textColor = imagecolorallocate($image, 0, 0, 0);

	// write the code on the background image
	imagestring($image, imageloadfont(DIR.'/images/verification/anonymous.gdf'), 5, 9, $rand, $textColor);

	// create the hash for the verification code
	// and put it in the session
	$_SESSION['image_random_value'] = md5($rand);

	// Date in the past 
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	// always modified 
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	// HTTP/1.1 
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	// HTTP/1.0 
	header("Pragma: no-cache");
	// send the content type header so the image is displayed properly
	header('Content-type: image/jpeg');

	// send the image to the browser
	imagejpeg($image);

	// destroy the image to free up the memory
	imagedestroy($image);

}

 

All of the code I have posted works perfectly well except that it does report the following errors;

 

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/username/public_html/functions/main.class.php on line 148

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/username/public_html/functions/main.class.php on line 150

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/username/public_html/functions/main.class.php on line 152

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/username/public_html/functions/main.class.php on line 153

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/username/public_html/functions/main.class.php on line 155

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/username/public_html/functions/main.class.php on line 157
ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC    $.' ",#(7),01444'9=82<.342ÿÛC  2!!22222222222222222222222222222222222222222222222222ÿÀ<–"ÿÄ ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ ?õ‹j¾%iô­`yjˆd³)”.AÎzÆ9皧e ê:•Ïˆ. “GÔ &Ý$fwb@Ü ã±àb¤–K} îõ«S¤^j’ynñdº³p¹ué…Ç\t]|™O@’K¯\kÚ”_Ù—3(Ky—þ=®pwÜœ’¥gi ÷¾4Ô®-à´ˆhÅ„¹h.ÇGØØÀ$¨<מ3ZÚ¤e41áÿMá•Z7Ó¯ïbLd‘¸‚¸ôlõâ«jßþkÝ/aP°¼p—32~cžžàÕ§™²wùéòþ¿à3™†ÓûCÆ“¤Zuì–öƒæ³78žDg9;XäzSGoíµKù€RÍw ¹3Fà‡L|Ûz7µkxkH‚ÛO›S»/ur|Ê¢¼´#?2î#pÇ'¦qߥfè°Üêþ%»Õã{û¸âl›‹FXîT…}ŸÅÀÁÖ·ß²FüË^É[úþ¾ó&%†ÓY¼´}P¬Èg³]ÈÀò7'R¼ò+Ú³#KwŽXK-ÆïÜÉ Ê¶8 ©>¹ëíZד5Ç‹Öh/'¹v™HšÖÛË”:ˆø‡§r)¾#”¦¸.c½Y%Y¤Šµ‘XyxÚþâ­&î—ë±”|Û·9òƒÆ˜;‚¡`¾Üdþ´ÂÓ& ªFí»‰zÖ®¿g$3ExÂý’åCy—…Éõ ¤«~•FèE,܉,£c…kx·+)È´^Àݵ#¶Óšñ"[-ÅéÜÏq`D£ø‹g£¢…ÕaŸL™­mÄŽn.uÎçUQ÷FO¯aɪúe¥Ä~!1Ûµù.Uí­'ÂÌ%w Žÿ_²‚ßþÛ¥¿ÒEÃD’ÂÀoŒ¸ O÷Gsê*dÌä÷ûÄE¼½ð½õÜñ®¥\,C³ 3;oÇVÕÂOâËSR‘ÞÓIŠòݾÉ"ãqm‰’;¼ÿÀ«:ßJ¶ŽÏH¸¾y§žúõeM6ß°äî;sÔãڷΛ«ø£Qi.›ìZN£|KÛp$) ãq=‡È׳mJIkýoøõ6ôÙ<7§ßË›X¶ê²*†Æ÷n»œLÖkø~Â}íZc´3²˜ŽÝÇvá»s‚9¢ÃÁz"A Ì«æf‹¢²–CòÆÐ繦^øgRðͬsé÷3ÌŽ–ö¯oÌù}Îû¿ýsY¦¯£9ÓŠ~ìµó6mÚSûJËÄp[¥Å½Ìea±‰ˆÀŸ_›ŽÔT^!±ƒÇZ«é­±Úåä Ãi*™FÈúí¢ˆ½7°A«jíäoÝE«jÇ[Ýi±ÆÆRØ 1€Ã<3éÔÖ&•{£M¬ßx†xî4©ú8{‘夊:Ÿî±Ýî~謯Ú蚯Âß]Ep!¼‰î›!I ŠªÝd{×u­ë:·À ipA rjóE¥Aq”²M±7u#+$^++ÛFsó[Oøx"6ÓÜø‹ÄÖˆÀíû=ý‘mÑGÑrÈÎsžG85çþ:mWTð~­âf™îm-öEcv®±KïÐe•prA#מ•BKàÌ ê²Ç-¾œñÞZ©,$>o“7ää3`㸫WÆÚ~ª|oá m'måµ´6ób öûÄ…Wv_¦+E{hmÌÚÓ{}ËËþ…¦xoRñy°dñˆ55°Ïr~ÚK@]G˜ª® ="´¯|'§ø[Q†âÛÄšÝì©&ïLDÝ@t(ISß•ªÉ¢ëÍ œwRÜ[^GÍ>ÏÃ×q‡µE33¢³H[jwqÏ#$5„¾ðN¥§êÚõðñ6“p¾EÄSÜ2Þ[¹Ë ’C’0H'Ž ü¿+Q¢³k@N1³’÷EÔoÃÖ2êWöOw©Ï/‘ok7›ÂV¤¹$’Hg#.]/WÖîÖMcÄš”Ó.Ôy÷‘GË»Ž:Œg©®Öñ?á1š:èZi±¨–Êm^ åK`ü“mà—óÖ³õïÅà‹D¶¾{Å Ougz$µÛÓseÉ$zúèßÚ‘Ñ&µ”¿áŽM¦¼Ñ5uðåíì:Œl‚kkËiw#.ÞA\㪞H sA´»Ó¼-$²¦§ÏÓ6È`aîÄî^=«’ð’Xëz̾(Õu½(\ÂËÒæŠFgÈe;ö¨ÏÞÈ*NIíŒTž6Òo.4û_ÛÙi°ÛYÊŠÐE.2»† RÛ±»çæÏšQ£~„Æ«;ôOð:­ÚöÆÆëR–ßXŠÈÆG›fQ‡»?o ªú”÷Rɨ46/emßoŸj1êæcì iEb¾-°†ãF²¶Òô¸ù2ÜêCäd`;ô®KEÓ`ñ‡5m@O¤éúf«1—tde€e<mäò7AŠ•OĹնYÑé·62\]êú”wJ¹Åo¦DbópáW¶:œþrMPÒô¿ìÛFßD—Y•|ÆŠB·B>oõ™ùGÇ~FGnSÅž$ÕüEâ[? iÚ—•`ŒO” àˆÔçv#'ËÊ‚Mt­ðãC¹ÊßCÔîµ·"{­Fêíãkmß6Kgi“¾Jõ焹·¡œê7umw·_ØŸµK Ëy«Eö?&Áï$ÜÊíå'@0>p0Ó9ô×ÐüE¨£¬Ú‚Úó¹b}{ûWðÒÒ×_ñ7ÄYæŽ_,jiwp’á”Ç{PåÒA)7¤õ]ÏHKáÍwí–ÖÎöbÐ[”;šRÅÙñÓO©Uß N×m¦j€K©Ú…’ì0È åŠàôéEK’NÌÎRIÚJìMbÚÇWÔôý#QÓf‘pnD¨ÄeãnáÏSíÐWΞµ¹>'Ñü-s~ š×Z¸¼šÔüË ÖñŒX¬•”àç ÔdúLɬZ¶©|òÁ{n«ºÒÚ Q° äç’r:u¥y]·,4¿\ëšÖ©uo/ˆÝ×ìBØÄmÌ’¬Œ È W§;³ŠmØI6Òéý6q×Ï<Ô¼3qf ¹¹ñ"ð_ƒüSoe¢M<=º¶‚ÚQæA¸†fbÁ±žÏê@#2‡þ(ÜMcñ:å­æìÿh{%’@ƒ€«&üçÕ”‚O=job9¹z~9¡é:µ×Ç¿ÙÏ«fçû9^êhc½\[Ÿ,Ã%GÐ{Ôzdú¾ŸñãÄ· ×1éè÷I Ÿ1nDh?¼~Eüë«ð×ÂËïxŽÿZÒucw=ìOn©z›ŒAÉ$€íòžŽO^ Zñ?…#ñˆtí[í÷šˆmßÈÓnbc*+ng’>›pÏÆGÞ;· -MÙ)¶uñëuí”ãQxb[XÒK嘅HI¶1íl$iÓJ>ó¸þô¢´m"—J‘ Ž9îÌí$òM#€‹ (Ïlt‹EW4–ˆ¥RqV‹Ð¦tÛÓlô­>þM8» „‰_2:çs) “ƒžjK‰u3â&?ÙðϦÛÕ0óL„8ú}êÕ–ÎÖKÈnÞÞ'¸J¤¬ ²‚y׬ü/á={QÓn§=Ó0ê„°nFz{öGP‡¿ýwÿØòè×¾ î4KÝ@”IŒl_î¯Ë÷Np <õ<Š»ýŸ­hðé:]œÑjV¥ÏÚ€å$îÎÉA<ŽMZ·tÖ4+ø!.d‘]2 *nZÑþƳ¶ÕçÕ¡%ÏÙ„XvmÎ~ïLð?*|Åsôþ»2ÿdj>'º»Ô´™íŸJU)rêB“÷·n^8ÆOBjî™>£qÖµ¥õ”ÊÍglÑ„ÀãæÏ#‘ÜU?ë—š÷†înµ’§Œü¸wcúVµç‡4Û·³ˆFöñ[K¹c¶s±¸/\ö`ÝŸ+ÿ3{ = Oø›L0ê€T·fsÏË·oñz“Þ´mVîÚY.dÖÜ“FMµŒª âQÆ nÅ3JÖ/gñÞ±¤Ë {[XcxQO^kb-2ÂM@jmgÛH)çmù€ƒÛŠNVÜR•´—õþG?wmqu§¥Ï‰´O·N’ ­—ïAÁ#'Ž§"’!£[jÖQËwuÛª´H™‚GǧN½Ï•=íõÞ‰ámKTŽæK›€ZE'r¦?„ŒjÜÓÕ.,-îå ó¦GÚ2xÛhr“Žð(-ž²w9Ô`¹œŒAnñíŠ.x$˜œw¤/©A=´rY%åÎ35æDq ä 9$ã·~æq¡ÙÃ¥5‘–É&•Llû]²yËš‚=VåV[röí<Þd¯Ã9êrNzôútÅ+“ÎïdRˆj:-»ù¯ ‚"ÅþõÅĤö*ŽÕx‹ME'µ›gža :©ù‘X}Üõæ—N¼–êâõ$Ûˆfظ½ýé÷v‘]®ÇÞƒzÈLlT±\ G#¸â•îÄÝÞ¤K՜䅒ØãŠÝp¢53zðz{ ’K{;ùbl‰ ´Å†Öà8|ÀuÆO^õ%´í3ÜÆ#”¢àv⦠a·(!x O\úÐMÊIs5”1Gw“ÎÛ˜µ¼D€7p?"áEh)$œö¢‹†‡ÿÙ

 

I have tried putting ob_start() at the top of the file, but this only makes the header errors disappear.  The output is still a load of gobble-de-goop.

 

Please note: GD is already installed & I already have the ShowSecurityImage() function working on it's own.

 

PLEASE HELP :(

Link to comment
Share on other sites

Hmmm yes I have recently realised that BUT even when I build the image before any HTML is returned - when I return the output only the CAPTCHA image is displayed and no HTML before or after it.

 

What would cause this?

 

If you like I can give you temporary FTP access and you can take a look at the full file - I will pay you.

 

Let me know.

Link to comment
Share on other sites

There is a freelance forum.

 

The reason only the image is displaying is because you're telling the browser that you are only going to give it image data by setting the headers like you are.

 

You want to isolate the CAPTCHA generator to it's own php file, then reference it using

 

<img src="captcha.php" alt="" />

 

You probably want to pre-define a CAPTCHA value and pass it to that script via sessions, or something equally secure.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.