English 中文(简体)
Different PHP results with -r option
原标题:
  • 时间:2009-11-18 20:35:34
  •  标签:
  • php
  • sha1

PHP appears to be giving different hash values when I use php -r <code> on the command line and when I execute the file with php <file>, php -f <file>, or run the code inside Apache.

For instance, a SHA1 usage on command line using -r:

$ php -r "print sha1( $1S*90 );"
77cd8b48ceca53e018f80536b0a44c5b6710425f

When I try the same with file testSHA.php below:

<?php
print sha1( $1S*90 );
?>

and run it on command line or inside Apache using mod_php5:

$ php testSHA.php
201cb5aaa7d4db1a49d9be1f2c06d45e4c2a69f2

Strangely, though, the hashes do match using the two methods when I try a different input string such as "123456789".

I don t think I am using a different encoding or character set in the two methods. I also tried using MD5 and still get different hashes on command line with -r and -f .

Could someone point out why the hashes would be different using the two methods above? Is there a way to run PHP on command line where I can type the code without entering it in a file, and see output as if it were run inside a file/Apache? I use the command line for quick snippet testing when step-through code debugging is not set up.

Thanks.

PS: I am using PHP 5.2.11 with Suhosin-Patch 0.9.7 (cli) on OpenSUSE 11.1.

最佳回答

When you run $ php -r "print sha1( $1S*90 );" on your bash shell, bash is interpreting $1 as a shell variable, which is probably empty unless you have set it.

So PHP sees just this: print sha1( S*90 );

The result of which is: 77cd8b48ceca53e018f80536b0a44c5b6710425f, the value you were getting first time. :)

You can escape it like this:

$ php -r "print sha1( $1S*90 );"
问题回答

The $ and or * in your string will be interpreted, maybe because of the " instead of





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签