Weird sprintf behavior in PHP - php

I was trying to do sprintf("<%s>", "Sat");, but nothing comes out. When you remove the less than symbol, it will start working again. Anyone experience this behavior and whether it expected? as i think it is a bug.
You can even get the same result here with printf.....
http://writecodeonline.com/php/

Your browser is probably rendering it as a tag. View source to confirm.
http://codepad.org/g5FXZAwa
<?php
printf("<%s>", "Sat");
Prints <Sat>
Edit for Yogesh.
<?php
echo sprintf("<%s>", "Sat");
Prints <Sat>

I believe that this happens because <Sat> is interpreted by your browser as a tag.

Related

Output modifier on modx placeholder is not working

I have this code in a template
[[+isShowMore:is=`1`:then=`show more`:else=`no`]]
It is printing no. But it should show show more as placeholder isShowMore is set to 1 by this line of code in a snippet.
$modx->setPlaceHolder('isShowMore', 1);
Also checked by this code
[[+isShowMore]]
[[+isShowMore:is=`1`:then=`show more`:else=`no`]]
[[+isShowMore]] is printing 1 but the line with output modifier showing no.
Any clue what is wrong here? I am using modx revolution 2.2.8 traditional.
Similar issue is also posted in modx forum.
http://forums.modx.com/thread/85150/output-filter-on-placeholder-problem#dis-post-469615
I had this problem; my page was using a template that had [[*content]]. Changing that to [[!*content]] to get rid of caching solved my issue.
FYI, my snippet is being called with ! so that its output isn't cached either.
Are you doing that conditional inside another conditional somehow? Nesting conditionals usually cause this type of weird problem. Have you tried calling both your snippet and the placeholder output uncached?
I've also experienced this several times and there doesn't seem to be an obvious cause, some unknown magic in the modx output conditional logic. Experience has taught me to simply try to avoid using them as much as I can.
It's ugly but perhaps you could work around your problem by placing whatever you wish to output in the actual placeholder and then just printing the placeholder as it is.
Not sure why this doesn't work in your case so I recommend you do it with a snippet.
[[EvalIsShowMore? &val=`[[+isShowMore]]`]]
in EvalIsShowMore snippet put something like
<?php
if($val){
echo 'something';
}else{
echo 'nothing';
}
Hm, probably your placeholder is located above snippet! 
In Modx output occurs at the last moment, but the logic works consistently (cascade). 
Here's an example of how to do: 
[[+isShowMore]]
[[!yourSnippet]]
[[+isShowMore:is=`1`:then=`show more`:else=`no`:toPlaceholder=`isShowMore`]]
another example: 
[[+snippet_placeholder1]]
[[!snippet]]
[[+snippet_placeholder1:add=`[[+snippet_placeholder2]]`:toPlaceholder=`snippet_placeholder1`]]
give the eq modifier a try:
[[+isShowMore:eq=`1`:then=`show more`:else=`no`]]

PHP: How to decode eval()?

I just noticed today that I have got lots of spam links in my wordpress blog. I just found a file which contains
<?php eval (chr(101).chr(114)...
Its very very long string. Can someone tell me how can I decode this to see what it does? So that I can try to remove the spam links?
Thanks.
Just replace eval by echo and have a look at the generated output
<?php echo (chr(101).chr(114)...
Instead of executing (eval) you can just echo out what it says, preferrably with htmlspecialchars if you execute it via browser:
<?php echo htmlspecialchars(chr(101)...
odds are though that you won't see anything understandable, since it is probably encoded in more ways than one.
Simply replace eval with echo:
<?php echo (chr(101).chr(114)...
Besides that, you most likely need to reinstall whatever you have on your webspace as you obviously have been hacked. Ensure that you use the most recent version of Wordpress and all other software you are running to prevent this from happening again.

Sending information through URL using $_GET not working

So I'm trying to do something extremely simple, and after reading through forums, and researching on google I still can't figure out why this is not working. But this is mostly like because I'm still a very much noobie programmer. I'm trying to send information through a url, and having a script pick it up using the $_GET super global.
Here's the link code, in a file called TESTFORM.php:
<p>
Here's a link:
ID
</p>
This is the TESTGET.php script:
<?php
if (isset($_GET['id']))
echo 'it is set<br />';
else
echo 'it is not set<br />';
?>
This yields in a "It is not set" appearing on the page every time. Any thoughts? Are there ghosts in my computer ruining my code? Thanks for taking the time to read through this! Happy coding!
I'm no PHP programmer, but I do know from HTML that computers (especially file names) don't "like" spaces. Try removing the spaces in the id = 5 code.
Your problem is the extraneous space here around the URL parameters:
ID
That will result in PHP seeing the parameter as $_GET["id_"]. The space gets converted into an underscore.
It's always best to use var_dump($_GET); or var_dump($_REQUEST) when you run into such problems. Secondarily it is sometimes helpful to get rid of isset in such cases. Albeit you have a custom error message in place of the language notices intended just for that.
Have you tried to remove spaces in your link?
ID
Code seems fine at a glance, have you tried removing the spaces in
?id = 5 to ?id=5

php generated option selected by default

I'm probably blind to my own mistake again, but why doesn't 2011 become the default selected year? (The -- are for debugging, they show up that the if statement works correctly.)
for($year=1900;$year<=2050;$year++) {
if ($year==date("Y"))
echo "<option value='".$year."' selected='selected'>--".$year."</option>";
else
echo "<option value='".$year."'>".$year."</option>";
}
The selected='selected' should just work. (link) So it is either a stupid mistake(i really don't see one) or some effect from php..
UPDATE:
found the problem.. the ftp didn't overwrite the file properly. Now it works. Thanks for the fast help, when these things happen I always start doubting my own sanity. (cant send this as answer because i have a low rep)
PHP has no effect on the browser behaviour, so if it doesn't work, it is a stupid mistake in the HTML output. ;)
Best thing is checking if the selected variant is actually outputted. Could be that the comparison evaluates to false, so it's always the second line. A little debugging should point out fast what is wrong. Check the output in the browser or var_dump the values of $year and date('Y') to see if they return what you expect.
At first sight I see no error here. Do you have any Javascript that might influence the selection?
CTRL + SHIFT + R if you use firefox.
I had that problem very often too ..
It should be just the cache.
Btw.: You should rather use double quotes for html attributes. That's more "usual". :)
Are you checking the generated HTML? Maybe this code works well (it seems it does), but the problem is in select, or in another place.

strange thing, ajax response includes whitespace

I ave several JS functions that call different php files with $.ajax jquery method... yesterday everything was fine, but after cleaning up my code, i don't know what i did, but now the ajax response is like "[space]data" instead of "data"..
i could use a trim function in Js but i want to fix the source of the problem...
all my php files have the missing last ?> in order to avoid that, and before <?php i'm sure, just checked, there is no space...
how come did I introduce this error? is the server? the browser?
The funny thing is that yesterday i cleaned my code with JSLINT..! bad idea..
thanks
When I had the same problem it was just a carriage return or space after the closing PHP tag, a surprisingly easy thing to introduce by accident.
Make sure you open the PHP tag at the start of the first line of your script, close it at the end and delete everything after the closed tag (should be easy to spot in a good editor).
I can see no reason why not closing your PHP tag wouldn't just be really annoying.. but thats just me!
I know I'm late this this thread, found it because I had the same issue. I was able to confirm that the although you may clear all the spacing in your callback function it is still possible to get these white-space/carriage returns in your response text (you can even see this in chrome developer tools under the "Network" tab).
So I tested and found that if you put ob_clean(); at the top of your callback function then it clears any of those spaces. I don't know much about this function just that it was mentioned in the codex (http://codex.wordpress.org/AJAX_in_Plugins#Debugging). Hope that helps anyone else that find there way here because of the same issue
If your response from the server includes the extra space, then the php code is the cause. If your response does not include the extra space, then the problem must be in javascript.
Use Fiddler to examine the actual response from the server to see if it really has the space in it. You'll then know for sure if the problem is with PHP or JS. Then you can post the relevant code.

Categories