sely
03-12-2007, 05:03 PM
is it possible to do a 301 redirect but have a delay?
|
Click to See Complete Forum and Search --> : 301 Redirect with delay? sely 03-12-2007, 05:03 PM is it possible to do a 301 redirect but have a delay? bokeh 03-12-2007, 05:29 PM Not in a single request. NightShift58 03-12-2007, 06:13 PM <?php $delay = 5; //seconds sleep($delay); header('HTTP/1.1 301 Moved Permanently'); header("Location: http://www.bokehman.com"); ?> bokeh 03-13-2007, 03:18 AM <?php $delay = 5; //seconds sleep($delay); header('HTTP/1.1 301 Moved Permanently'); header("Location: http://www.bokehman.com"); ?>This thread started here: http://www.webdeveloper.com/forum/showthread.php?t=140108 He wants to display output to a client for a period and then send a 301 header to redirect them. The code you have posted works for the question as proposed in this thread but cannot work in output context. MrCoder 03-13-2007, 07:29 AM Use javascript to reload a page after displaying the text. The reloaded page would contain the headers. bokeh 03-13-2007, 07:37 AM Use javascript to reload a page after displaying the text. The reloaded page would contain the headers. That defeats the purpose of a 301 redirect. Either content exists and there is no permanant redirect or it does not exist and there is a permanant redirect but it cannot be both. MrCoder 03-13-2007, 07:46 AM lol, good point! NightShift58 03-13-2007, 08:18 AM This thread started here: http://www.webdeveloper.com/forum/showthread.php?t=140108 He wants to display output to a client for a period and then send a 301 header to redirect them. The code you have posted works for the question as proposed in this thread but cannot work in output context.You're right. If he really needs to do something like that, he's down to doing an out-of-sequence <meta refresh> after displaying the output, which won't interfere with any headers already sent - but he can't do a 301. bokeh 03-13-2007, 08:30 AM <meta refresh>That method should be shunned. Use: header("Refresh: $seconds; URL=\"$location\""); NightShift58 03-13-2007, 08:38 AM That method should be shunned. Use: header("Refresh: $seconds; URL=\"$location\"");They're identical. bokeh 03-13-2007, 09:17 AM They're identical.That's debateable. Firstly headers don't break the "back button" and secondly they do not prevail in a saved document. NightShift58 03-13-2007, 04:30 PM http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm bokeh 03-13-2007, 04:44 PM From W3C: Checklist item: Avoid using meta http-equiv of "refresh; url=..." to automatically redirect users. http://www.w3.org/TR/WCAG10-CORE-TECHS/#auto-page-refresh NightShift58 03-13-2007, 04:58 PM I agree. Same-same for header("Location") and location.replace(). Avoid using either. But just as I avoid the taxman doesn't mean I can always do so. Still, the use of header(), location.replace() and <meta refresh> should be avoided. They are often used as crutches for an ill-defined program flow. bokeh 03-13-2007, 05:12 PM I agree. Same-same for header("Location") Header location is ok to use. It's meaningful and has a status code. One other thing that link points out is headers can be used, for example, in text/plain documents whereas the meta element would be completely meaningless. NightShift58 03-13-2007, 05:28 PM I have to disagree on the use of redirects, in general. They are simply crutches for - often - poor design. Every application, module, script, function, loop, etc., should have a well defined single point of entry and an equally well defined single exit point. In a morbid example, imagine a coroner trying to find out what really happened when s/he sees one bullet hole going in and 3 going out... I realize that there are sometimes valid reasons for using redirects. But these are truly very seldom. Unless trying to justify a coding shortcut. In most large conventional development, you can't get away with a function that has more than 1 return - QA won't let it go through and will send you back to your keyboard. I realize that PHP is a very modern thing and internet programming, in general, is considered modern, new, etc. But it's not really true. When you code in PHP for a browser, syntax aside, one isn't doing anything much different today than one did 20 years ago (or today, still) with a conventional COBOL/CICS application. The basic programming rules of yesteryear still apply - as this forum often demonstrates. Taschen 03-13-2007, 06:07 PM Perhaps slightly disingenuous to compare a post mortem with programming a web application. I have to admit to having used url-redirect as a short cut. However, it is important to distinguish bet url-redirect and location - perfectly legitimate. NightShift58 03-14-2007, 12:15 AM Perhaps slightly disingenuous to compare a post mortem with programming a web application. I have to admit to having used url-redirect as a short cut. However, it is important to distinguish bet url-redirect and location - perfectly legitimate.I'm still learning as I go and I spend enough time doing post mortems on applications to never forget that any script/module coded will have to be debugged - sooner than later. I can only repeat that I realize that there are occasions when there is no option but to move the visitor from one page to another. I have my doubts about the need to do it as often as seems to be the case in this forum. And, if you look at some of the script structures, one can only wonder sometimes. In Germany, most of the freeways have no speed limits. Do I speed because I didn't leave in time? Or do I leave late because I know I can speed? (Which could take us back to the post mortem bit). How do I structure my application? Do I have to worry about getting it "right", knowing that there'll be a shortcut I can take somewhere, later? Still, never say "never", but maybe "not as often"? NightShift58 03-14-2007, 01:37 AM No more edit buttons... it's a shame... Here's a case in point: http://www.webdeveloper.com/forum/showthread.php?p=727569 Is this use of header("Location") acceptable in your eyes? bokeh 03-14-2007, 03:56 AM Is this use of header("Location") acceptable in your eyes?Well the thread is not clear and the use in the tutorial is one I certainly wouldn't agree with but what is being suggested in the thread (POST, REDIRECT, GET) seems to have become standard practice to avoid accidental resubmissions of non idempotent requests. The RFC says: The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field. I always used to have the same script produce the form, failure and success pages and still do but often on success now I do:die(header('Location: '.$_SERVER['PHP_SELF'].'?success'));That's two bugbears in one for you though as it contains another exit point which brings us to that: I believe having multiple exit points allows code to be more efficient. Here's an example:function GetHandCategory($score) { if($score > 6185) return 1; if($score > 3325) return 2; if($score > 2467) return 3; if($score > 1609) return 4; if($score > 1599) return 5; if($score > 322) return 6; if($score > 166) return 7; if($score > 10) return 8; return 9; }function GetHandCategory($score) { $i = 1; if($score < 10) ++$i; if($score < 166) ++$i; if($score < 322) ++$i; if($score < 1599) ++$i; if($score < 1609) ++$i; if($score < 2467) ++$i; if($score < 3325) ++$i; return $i; }Obviously example 1 is doing a lot less work, and we can order the conditions to match the probabilty of the input value, cutting the work even further. Or imagine for example we are trying to brute force an MD5 żDo we stay in the loop for "N" billions of iterations, even though we have already found a match, just so we don't have multiple exit points? Taschen 03-14-2007, 05:02 AM Is this use of header("Location") acceptable in your eyes? Not really, although I do use a form scrubber/mailer that utilises different URLS on success (via location) depending on which form was submitted. Maybe it's time to tighten up that part of the application. NightShift58 03-14-2007, 08:11 PM The following yields a difference of .20 to .25 seconds per 100K iterations, the "winner" being the "unstructured" script. I would take the lesser performance over the slightly faster one in this case, because it affords me more coding flexibility in the short-mid-long term. The "structured" version is not harder to code, doesn't distort the original logic and is equally "legible". It's also easier to debug, if and when necessary.<?php $arrRAND = array(); FOR ($x = 0; $x <= 9; $x++) : $arrRAND[] = mt_rand(0,8000); ENDFOR; $loops = 100000; echo "<hr>"; $time = microtime(true); FOR ($y = 0; $y < $loops; $y++) : FOR ($x = 0; $x <= 9; $x++) : $result = GetHandCategory2($arrRAND[$x]); ENDFOR; ENDFOR; echo microtime(true) - $time; echo "<hr>"; $time = microtime(true); FOR ($y = 0; $y < $loops; $y++) : FOR ($x = 0; $x <= 9; $x++) : $result = GetHandCategory($arrRAND[$x]); ENDFOR; ENDFOR; echo microtime(true) - $time; echo "<hr>"; function GetHandCategory($score) { if($score > 6185) return 1; if($score > 3325) return 2; if($score > 2467) return 3; if($score > 1609) return 4; if($score > 1599) return 5; if($score > 322) return 6; if($score > 166) return 7; if($score > 10) return 8; return 9; } function GetHandCategory2($score) { if ($score > 6185) $retVAL = 1; elseif ($score > 3325) $retVAL = 2; elseif ($score > 2467) $retVAL = 3; elseif ($score > 1609) $retVAL = 4; elseif ($score > 1599) $retVAL = 5; elseif ($score > 322) $retVAL = 6; elseif ($score > 166) $retVAL = 7; elseif ($score > 10) $retVAL = 8; else $retVAL = 9; return $retVAL; } ?> bokeh 03-15-2007, 03:50 AM How would you apply your "structured, single exit point ideology" to something like the following without getting ineffcient?function BruteForceMD5($md5, $charset, $max_len) { $Sleepy=array_merge(array(null), str_split($charset)); for($Happy=0;$Happy<$max_len;++$Happy) { @$Dopey.='for($Sneezy'.$Happy.'=0,$Bashful=count($Sleepy);$Sneezy'.$Happy.'<$Bashful;++$Sneezy'.$Happy.')'; @$Grumpy.=($Happy?'.':'').'$Sleepy[$Sneezy'.$Happy.']'; } return eval($Dopey.'if($md5===md5($Doc='.$Grumpy.'))return$Doc;return false;'); }I suppose you could use break and more unnecessary code to handle a false return where necessary. From an efficiency point of view more exit points is always going to be better. Imagine you are in a building on fire, żdon't you want as many exits as possible? żOr do you want to be under the constrains that some third party who "knows whats best" has imposed upon you? NightShift58 03-15-2007, 05:04 AM How would you apply your "structured, single exit point ideology"Easy.... For one, it's not ideology. It's just the way larger projects work, the kind that cannot afford to fail - industry, banking, government, etc. Second, as the first point implies, it's not "mine". I try to adhere to it after years of being required to work that way. It's a good methodoly - not ideology - and it works. If you Google "structured programming", you'll get 21 million possible results. It's nothing new but, after all these years, it's still the prevalent methodoly for large scale, professional projects. Most programming project support tools also support this apporach, which helps. function BruteForceMD5($md5, $charset, $max_len) { $Sleepy=array_merge(array(null), str_split($charset)); for($Happy=0;$Happy<$max_len;++$Happy) { @$Dopey.='for($Sneezy'.$Happy.'=0,$Bashful=count($Sleepy);$Sneezy'.$Happy.'<$Bashful;++$Sneezy'.$Happy.')'; @$Grumpy.=($Happy?'.':'').'$Sleepy[$Sneezy'.$Happy.']'; } return eval($Dopey.'if($md5===md5($Doc='.$Grumpy.'))return$Doc;return false;'); } This is wizardry and it's fun. In most team projects, you couldn't get away with this. Starting with the variable name and ending with the eval() From an efficiency point of view more exit points is always going to be better.Actually, the contrary. Of course, that depends upon which aspects of efficiency one focuses: cheap CPU's or expensive programmers? Also, the larger the programming team, the higher the costs of not applying structured methodologies and the higher the failure rates.Imagine you are in a building on fire, żdon't you want as many exits as possible?In fact, a lot of the code in these hallowed halls DOES seem to have been written while the building was on fire. First coded, then the vital questions asked. What you describe as a building with many exits is in fact a very modular, structured and carefully conceived thing. I understand the rhetoric behind the question, but, in programming, the many exits is what usually causes the fires. żOr do you want to be under the constrains that some third party who "knows whats best" has imposed upon you?When I started out in this field, I thought most of the projects were overburdened with such rules. I thought it was useless, that they were restraining our "creativity", etc. When I started my own business a few years later, in less than a year I was imposing even more stringent rules than what I had been complaining about. Economically, it jut doesn't make sense to proceed otherwise: teams need structured approaches to problem solving. And it's not Nightshift who invented this. I'm just a believer - and I wish I was more consistent in my own coding and less prone to taking the very shortcuts I'm publicly decrying. As far as the third party goes, often he/she is also the paying party. If a customer wants to protect his investment and requires sound and proven programming practices, who can blame him/her? bokeh 03-15-2007, 05:31 AM ending with the eval()The function is dynamic. If I am only checking to a maximum of 3 characters my eval code looks like this:for($Sneezy0=0,$Bashful=count($Sleepy);$Sneezy0<$Bashful;++$Sneezy0)for($Sneezy1=0,$Bashful=count($Sleepy);$Sneezy1<$Bashful;++$Sneezy1)for($Sneezy2=0,$Bashful=count($Sleepy);$Sneezy2<$Bashful;++$Sneezy2)if($md5===md5($Doc=$Sleepy[$Sneezy0].$Sleepy[$Sneezy1].$Sleepy[$Sneezy2]))return$Doc;return false;Whereas if I am checking up to a maximum of 6 my eval code looks like this:for($Sneezy0=0,$Bashful=count($Sleepy);$Sneezy0<$Bashful;++$Sneezy0)for($Sneezy1=0,$Bashful=count($Sleepy);$Sneezy1<$Bashful;++$Sneezy1)for($Sneezy2=0,$Bashful=count($Sleepy);$Sneezy2<$Bashful;++$Sneezy2)for($Sneezy3=0,$Bashful=count($Sleepy);$Sneezy3<$Bashful;++$Sneezy3)for($Sneezy4=0,$Bashful=count($Sleepy);$Sneezy4<$Bashful;++$Sneezy4)for($Sneezy5=0,$Bashful=count($Sleepy);$Sneezy5<$Bashful;++$Sneezy5)if($md5===md5($Doc=$Sleepy[$Sneezy0].$Sleepy[$Sneezy1].$Sleepy[$Sneezy2].$Sleepy[$Sneezy3].$Sleepy[$Sneezy4].$Sleepy[$Sneezy5]))return$Doc;return false;How could I do that without the eval? NightShift58 03-15-2007, 05:51 AM It's 04:49 a.m. and I'm headed for bed, but I promise to look at it. As an interim answer, I can only say that there quite a few languages that do not have the eval() function or a similar function. And problems still get solved... NightShift58 03-15-2007, 06:08 AM The function is dynamic. If I am only checking to a maximum of 3 characters my eval code looks like this:for($Sneezy0=0,$Bashful=count($Sleepy);$Sneezy0<$Bashful;++$Sneezy0)for($Sneezy1=0,$Bashful=count($Sleepy);$Sneezy1<$Bashful;++$Sneezy1)for($Sneezy2=0,$Bashful=count($Sleepy);$Sneezy2<$Bashful;++$Sneezy2)if($md5===md5($Doc=$Sleepy[$Sneezy0].$Sleepy[$Sneezy1].$Sleepy[$Sneezy2]))return$Doc;return false;Whereas if I am checking up to a maximum of 6 my eval code looks like this:for($Sneezy0=0,$Bashful=count($Sleepy);$Sneezy0<$Bashful;++$Sneezy0)for($Sneezy1=0,$Bashful=count($Sleepy);$Sneezy1<$Bashful;++$Sneezy1)for($Sneezy2=0,$Bashful=count($Sleepy);$Sneezy2<$Bashful;++$Sneezy2)for($Sneezy3=0,$Bashful=count($Sleepy);$Sneezy3<$Bashful;++$Sneezy3)for($Sneezy4=0,$Bashful=count($Sleepy);$Sneezy4<$Bashful;++$Sneezy4)for($Sneezy5=0,$Bashful=count($Sleepy);$Sneezy5<$Bashful;++$Sneezy5)if($md5===md5($Doc=$Sleepy[$Sneezy0].$Sleepy[$Sneezy1].$Sleepy[$Sneezy2].$Sleepy[$Sneezy3].$Sleepy[$Sneezy4].$Sleepy[$Sneezy5]))return$Doc;return false;How could I do that without the eval?Ok, so I lied and didn't go to bed yet... You're using eval() to produce a recursive effect, which you could achieve just as well through normal recursion. sely 03-15-2007, 11:30 AM the ultimate goal is to be able to permanently redirect so i can benefit from search engine ranking of the old site, etc... BUT because of the nature of the switch in websites, i need to be able to display a page of content before the redirect occurs. can i do this with this code?... <?php $delay = 5; //seconds sleep($delay); header('HTTP/1.1 301 Moved Permanently'); header("Location: http://www.bokehman.com"); ?> sely 03-15-2007, 11:53 AM the ultimate goal is to be able to permanently redirect so i can benefit from search engine ranking of the old site, etc... BUT because of the nature of the switch in websites, i need to be able to display a page of content before the redirect occurs. can i do this with this code?... <?php $delay = 5; //seconds sleep($delay); header('HTTP/1.1 301 Moved Permanently'); header("Location: http://www.bokehman.com"); ?> bokeh 03-15-2007, 01:11 PM the ultimate goal is to be able to permanently redirect so i can benefit from search engine ranking of the old siteNo! You can't have your cake and eat it too. If you have an intermediate page the 301 redirect will apply to that page and not the source page. NightShift58 03-19-2007, 01:45 AM As I try to decipher the function, I did come across some some redundancies in the current code, which with a $max_len of 4 will cost you close to .8 seconds. Here's a version with some of the redundant code removed/fixed:function BruteForceMD5($md5, $charset, $max_len) { $cnt = strlen($charset); FOR ($x=0; $x < $max_len; ++$x) : @$Dopey .= 'for($l'.$x.'=0;$l'.$x.'<'.$cnt.';++$l'.$x.')'; @$Grumpy.=($x?'.':'').'$charset[$l'.$x.']'; ENDFOR; return eval($Dopey.'if($md5===md5($Doc='.$Grumpy.'))return$Doc;return false;'); }Basically, there's no need for $Bashful's omnipresence nor for $Sleepy existence. bokeh 03-19-2007, 07:57 AM As I try to decipher the function, I did come across some some redundancies in the current code, which with a $max_len of 4 will cost you close to .8 seconds. Here's a version with some of the redundant code removed/fixed. Basically, there's no need for $Bashful's omnipresence nor for $Sleepy existence.$Sleepy=array_merge(array(null), str_split($charset));That is necessary so that $max_lenworks as the maximum length rather than just the length. $bashful is needed because we cant't handle the NULL in string context. For example your function would fail for the following arguments:echo BruteForceMD5(md5('dog'), 'defghijklmno', 4); NightShift58 03-19-2007, 03:42 PM Not sure I follow. First $Bashful... There's no need to include "$Bashful=count($Sleepy)" followed by "$Sneezy'.$Happy.'<$Bashful;++$Sneezy'.$Happy.'". The length/count of $Sleepy is invariant within the function and there's therefore no need to pass this on to the eval string as if it were. It should be used as a literal numeric value in the eval string, which saves you from re-evaluating it on every iteration of every loop in the eval string. Something like:function BruteForceMD5($md5, $charset, $max_len) { $Sleepy=array_merge(array(null), str_split($charset)); $Bashful = count($Sleepy); for($Happy=0;$Happy<$max_len;++$Happy) { @$Dopey.='for($Sneezy'.$Happy.'=0;$Sneezy'.$Happy.'<'.$Bashful.';++$Sneezy'.$Happy.')'; @$Grumpy.=($Happy?'.':'').'$Sleepy[$Sneezy'.$Happy.']'; } return eval($Dopey.'if($md5===md5($Doc='.$Grumpy.'))return$Doc;return false;'); }Now $Sleepy... Essentially, $Sleepy = $charset. If $charset == NULL, we don't need to do anything, as there's nothing to work with, so the whole content of the function should be wrapped in an IF-ELSE-ENDIF. $charset is a string and as such, addressable as an array of characters. We don't "need" $Sleepy[$Sneezy'.$Happy.'] because $charset[$Sneezy'.$Happy.'] would work just as well. function BruteForceMD5($md5, $charset='', $max_len=4) { $EVALstr = "return false"; $cnt = strlen($charset); IF ($cnt) : FOR ($x=0; $x < $max_len; ++$x) : @$Dopey .= 'for($l'.$x.'=0;$l'.$x.'<'.$cnt.';++$l'.$x.')'; @$Grumpy.=($x?'.':'').'$charset[$l'.$x.']'; ENDFOR; $EVALstr = $Dopey.'if($md5===md5($Doc='.$Grumpy.'))return$Doc;return false;'); ENDIF; return eval($EVALstr); } As for the non-eval version, I've got something in the works but it's still brutal bruteforce, as I'm generating ALL the permutations and not just within $max_len. Maybe 1-2 days, as I've been tied up lately. bokeh 03-19-2007, 04:13 PM Not sure I follow.Your code has a parse error in it but even if that is removed it still doesn't work. The null is there for a reason. If max_len = 4 and the hash is 1, 2 or 3 characters long without a null in the charset it is not possible to run the shorter strings. NightShift58 03-19-2007, 05:01 PM Your code has a parse error in it but even if that is removed it still doesn't work. The null is there for a reason. If max_len = 4 and the hash is 1, 2 or 3 characters long without a null in the charset it is not possible to run the shorter strings.I get it. It's ok, because there's no "penalty" in using the array instead of array of characters and if it's needed... The part that is costing time, though, is the repeated re-evaluation of $Bashful/count(). The length/size of the array does not change within the function. bokeh 03-19-2007, 06:00 PM I did come across some some redundancies in the current code, which with a $max_len of 4 will cost you close to .8 seconds.Where did you get that .8 second (800 millisecond) figure? I agree that there is a minor inefficiency there but a whole 4 char run only takes 750 milliseconds on my machine. Also I did a test to see how many times count could be used in 800 milliseconds. On my machine that figure is 1.25 million times whereas the function is using it a fraction of those times.$array = str_split('abcdefghijklmnopqrstuvwxyz'); $start = microtime(true); for($i=0; $i<1250000;$i++) count($array); echo (round(microtime(true)-$start, 3)*1000).' milliseconds!';Also, I don't see any recursive function getting anywhere near the efficiency of the for loops. NightShift58 03-19-2007, 08:00 PM I ran it on a "public" machine (Bravenet/nightshift58.com), which by its nature is going to be slower than your dedicated PC in the home office, even if it may - or may not - have a faster CPU than yours. I also think/know that code executed by eval has different performance characteristics than code run "natively" - for lack of a better word - and using a literal for an invariant is more than likely to give you an edge over both assigning a variable within the eval() and then using it. There's nothing wrong with your function. The discussion started elsewhere really. Still, not all languages have eval() or similar function. Yet, restricted permutation problems are able to be solved. Most (if not all) of the algorithms involve the use of recursion, just as you are using it. When you refer to efficiency, you must be referring to the lines of codes needed to produce the desired result. Otherwise, eval() can't be considered "efficient". bokeh 03-19-2007, 08:07 PM The process eval in php is the same as running an include file except without the overhead of the disc access. If eval didn't exist you could write the code to a file and then include it. NightShift58 03-19-2007, 09:37 PM If eval didn't exist you could write the code to a file and then include it.The question doesn't really pose itself because you can only do that with PHP and some other interpreted languages, which have eval() or similar function, but you couldn't apply this algorithm or solution to a compiled language. Here's a "structured" solution, which, as you can see, doesn't involve much more coding. I was working on a slightly different angle but this one is much better, algorithmically speaking. See: http://www.xtremevbtalk.com/showpost.php?p=549189&postcount=8 This leaves the task open of implementing it in PHP. I'll do it over the coming days, as I'm tied up with a number of projects. bokeh 03-20-2007, 04:35 AM That's slightly different because is searching permutations (or combinations with repetition) rather than combinations (without repetition). Permutations is far easier. NightShift58 03-20-2007, 06:46 PM There are examples for both in that thread, based on the same basic handling of "depth of loop". bokeh 03-21-2007, 03:31 AM I'm getting confused between two pieces of code I wrote. This one is just a basic for loop but the other is a lot more complicated because it avoids repetition.function GetCombos($input, $size = 3) { if($size === 0) return array(); $input = array_values($input); $code = ''; $cnt = count($input); $ret = array(); $i0 = -1; for($i=0;$i<$size;++$i) { $k = 'i'.($i+1); $code .= 'for($'.$k.'=$i'.$i.'+1; $'.$k.'< $cnt-'.($size-$i-1).'; ++$'.$k.') '; } $code .= '$ret[] = array($input[$i'.implode('], $input[$i',range(1,$size)).']);'; eval($code); return $ret; }for($i1=$i0+1; $i1< $cnt-3; ++$i1) for($i2=$i1+1; $i2< $cnt-2; ++$i2) for($i3=$i2+1; $i3< $cnt-1; ++$i3) for($i4=$i3+1; $i4< $cnt-0; ++$i4) $ret[] = array($input[$i1], $input[$i2], $input[$i3], $input[$i4]);See the difference? The first one should be easy because it is just a straight forward for loop, but I don't know about the second. I think recursion would be extremely slow though conpared to the for loops. bokeh 03-21-2007, 04:31 AM After giving this too much thought there is no reason why eval should not be used. Eval is a tool of the PHP language and whether it may or may not exist in other languages is irrelevent to PHP. There seems to be a ladder of social superiority when it come to using eval similar to that found amongst the OOP brigade and the only argument ever presented is "what if" (again based on superiority). The only valid argument against using eval is that one of the expanded variables might contain some dangerous code but this is not a problem with eval but with the programmer that does not properly validate his inputs. NightShift58 03-22-2007, 03:28 AM I don't know whose superiority you mean. I never claimed that. I said there was nothing wrong with your script. I said your script amounted to "wizardry". The discussion began with 301 Redirect, the (in my view) improper use of header() as a program flow mechanism, etc, and moved on to structured programming. Your question to me was: "How can this be solved without using eval()?" My answer was and is: recursion. It has nothing to do with superiority, at least not from my side. It's true that eval() can expose scripts to security risks but, it just requires the same attention as any SQL bound user input. Personally, I'm not concerned about the use of eval(). It's there for a purpose and, used properly, I don't see why it shouldn't or couldn't be used. That wasn't my point. In fact, in some projects I've done, I had to use a PHP encoder and that is really nothing but a giant eval(). There is also no reason why eval() can't be used in a structured program. Again, that wasn't my initial point. I was going off on the single-entry, single-exit thing and eval() does nothing else but execute code in a particular spot in a script, similar to the way (as you pointed out) an include would do the same. Neither of these affect the single-entry, single-exit principle, provided they are coded accordingly. In other words, use of the same doesn't disqualify. The main thrust of my ramblings was that structured programming is nothing new and has a proven track record: it saves time, money and lots of aggravation - this forum being a living testimony to that. bokeh 03-22-2007, 07:17 AM I don't know whose superiority you mean. I never claimed that.Superiority of the employer over the employee; I wasn't directing that at you. webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |