robindean Posted May 11, 2009 Share Posted May 11, 2009 Ampersands are causing chaos for my pages. If I don't use "&" in a url, my pages won't validate. If I do, php appends only the "amp;" part to each of my variables. So, I've devised a solution which is yet to be complete (I need help). Basically, I'm starting off with ... $query = str_replace('&', '&', $_SERVER['QUERY_STRING']); This grabs the entire query and replaces "&" with simply "&". I need a way to locate specific variable values within this, using a method similar to $_GET. Is there a $_GET method for strings? If not and a function is necessary, please guide me through it. The function call should work in a manner such as ... $myVariable = GetVarValue('Variable_Name'); How? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 explode Quote Link to comment Share on other sites More sharing options...
gevans Posted May 11, 2009 Share Posted May 11, 2009 If I don't use "&" in a url, my pages won't validate Care to elaborate? What are you tring to do. Are you sending a GET query from one page to another? is the ampersand there for splitting your query, part of a string, or a messed up file name... Quote Link to comment Share on other sites More sharing options...
Michdd Posted May 11, 2009 Share Posted May 11, 2009 If I don't use "&" in a url, my pages won't validate Care to elaborate? What are you tring to do. Are you sending a GET query from one page to another? is the ampersand there for splitting your query, part of a string, or a messed up file name... I think the person is referring to xhtml validation? http://validator.w3.org/ Quote Link to comment Share on other sites More sharing options...
robindean Posted May 11, 2009 Author Share Posted May 11, 2009 It's a simple thing, really. HTML validation requires that url requests use "&" in place of "&". So, rather than going through a headache process of switching between the two, I simple grab the entire query string (via php on server side) and convert it for easier use. ACCEPT ... I don't know how to use it! I'm not convinced about explode, as it creates bunch of difficulty with becoming an array. I'd rather go about it by finding the variable we need the value for within the string and grabbing ONLY the value. So, if the string is "display=yes&tomorrow=100&you=helpful" ... and I want to grab the VALUE of tomorrow (100) from what is now a STRING (can't use $_GET anymore) ... how? Quote Link to comment Share on other sites More sharing options...
gevans Posted May 11, 2009 Share Posted May 11, 2009 Your URL will not cause a page to not validate due to a & in the query string. & is used to seperate queries sent via the GET superglobal. That way you will not need to split your query string as you'll have access to all your variables via $_GET Quote Link to comment Share on other sites More sharing options...
Ayvah Posted May 11, 2009 Share Posted May 11, 2009 You're on the right track with the validation. However, changing the & should only affect the source code -- & should automatically be converted into & by the browser before it requests the URL. If your $_GET has been affected then you've done something wrong. If & is appearing in the URL request, then it's likely you've double-escaped it. In order to generate the correct HTML you can use either htmlentities() or htmlspecialchars() on any text/data entered into the HTML. Quote Link to comment Share on other sites More sharing options...
robindean Posted May 11, 2009 Author Share Posted May 11, 2009 Okay, well, my exact html (one example) reads as follows ... <div id="curtain" style="width:100%;height:100%;position:fixed;top:0;left:0;z-index:2;background:#5d2e23 url(index.php?display=img&w=1&h=1&c=5d2e23);"></div> ... if you look at the url() part for the style attributes ... where exactly am I going wrong with this code? How am I to correct it? I've read about htmlspecialchars() but not htmlentities() and couldn't really grasp what I was supposed to do (or where) with it. Please keep posting so that I might solve this issue Quote Link to comment Share on other sites More sharing options...
gevans Posted May 11, 2009 Share Posted May 11, 2009 Have you tried just wrapping it in quotes? url('index.php?display=img&w=1&h=1&c=5d2e23') Quote Link to comment Share on other sites More sharing options...
robindean Posted May 11, 2009 Author Share Posted May 11, 2009 That doesn't seem to have any effect. I believe my problem might be stemming from this part of my code (but I have NO idea where or why) ... $display = $_GET['display']; if ($display == 'img') { header("Content-type: image/png"); $f = $_GET['f']; $w = $_GET['w']; $h = $_GET['h']; if ($f != null) { $t = substr($f, 0, -4).'_'.$w.'x'.$h.'.png'; if (file_exists($t)) header('location: '.$t); else { list($wo, $ho) = getimagesize($f); $ip = imagecreatetruecolor($w, $h); $img = imagecreatefrompng($f); imagecopyresampled($ip, $img, 0, 0, 0, 0, $w, $h, $wo, $ho); imagepng($ip, $t, 9); imagepng($ip, null, 9); } } else { $c = strtolower($_GET['c']); if ($w == $h) $t = 'bg/bg_'.$c.'.png'; else if ($w < $h) $t = 'bg/bg_hr_'.$c.'.png'; else $t = 'bg/bg_vr_'.$c.'.png'; if (file_exists($t)) header('location: '.$t); else { $img = @imagecreate($w, $h); sscanf($c, "%2x%2x%2x", $r, $g, $b); $sc = ImageColorAllocate($img, $r, $g, $b); if ($w != $h) { $a = imagecolorallocatealpha($img, 0, 0, 0, 127); if ($w < $h) ImageSetPixel($img, 0, 1, $a); else if ($w > $h) ImageSetPixel($img, 1, 0, $a); } imagepng($img, $t, 9); imagepng($img, null, 9); } } } Anything standing out to you? Quote Link to comment Share on other sites More sharing options...
Ayvah Posted May 11, 2009 Share Posted May 11, 2009 htmlspecialchars() encodes any characters reserved for HTML code. htmlentities() does the same, but it also encodes anything it can -- things like Chinese letters and whatnot that aren't ASCII. It's not as important if you're using UTF8. You'll get the best results with validation if you combine UTF8 and htmlspecialchars(). First let's work out where the problem is. You can temporarily insert the following at the beginning of that code. Then we'll have a better idea of where everything is going wrong. var_dump($_GET); die("\n".$_SERVER["QUERY_STRING"]); Quote Link to comment Share on other sites More sharing options...
robindean Posted May 11, 2009 Author Share Posted May 11, 2009 I get the following for http://localhost/index.php?display=img&w=1&h=1&c=5d2e23 ... array(4) { ["display"]=> string(3) "img" ["w"]=> string(1) "1" ["h"]=> string(1) "1" ["c"]=> string(6) "5d2e23" } display=img&w=1&h=1&c=5d2e23 and then when I type & in place of & for the address bar, I get ... array(4) { ["display"]=> string(3) "img" ["amp;w"]=> string(1) "1" ["amp;h"]=> string(1) "1" ["amp;c"]=> string(6) "5d2e23" } display=img&w=1&h=1&c=5d2e23 Quote Link to comment Share on other sites More sharing options...
robindean Posted May 11, 2009 Author Share Posted May 11, 2009 I think I found the problem. It was with my css / javascript file. From external .css and .js ... I'm supposed to go ahead and use plain old "&" in the url right? It's only for plain text / html that & is required? At any rate, the browser isn't showing the url related errors any more (iCab for mac shows when a url isn't encoded correctly). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.