2

is there any way to get js variable in php, i try something funny like this:

<script>
var x = "random text";
alert("aaa<?php echo md5(;?>"+x+"<?php); ?>");
</script>

Of course it gives me error.

Parse error: parse error, expecting `')'' in X:XXX\index.php on line XXX

If there is a way without refreshing the page or using GET parameters, please let me know. Thanks

EDIT:

What I am trying to do is: hash(md5 inside php) the javascript variable.

2
  • You cannot do this, what you're doing is embedding PHP into your JavaScript, and that just doesn't work. Best bet would be to AJAX it to return the md5. Commented Nov 15, 2010 at 10:31
  • 2
    @ILMV: or just use one of the JavaScript-based MD5 solutions out there... Commented Nov 15, 2010 at 10:34

3 Answers 3

5

PHP runs at the server, JavaScript runs at the client (browser). The only way you can pass a variable from JavaScript to PHP is sending it via an AJAX request.

There are several MD5 solutions out there for JavaScript, but it's not the same if you want the variable hashed before it reaches the user.

Sign up to request clarification or add additional context in comments.

Comments

3

You can't do it like that because PHP builds the page before javascript is initialized. You'll have to do an AJAX request.

<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var x = "random text"
$.post('md5.php', {md5:x}, function(data) {
  alert(data);
});
</script>

<?php
  echo md5($_POST['md5']);
?>

Or..

function md5(e){function k(h,i){var g=[h&2147483648,i&2147483648,h&1073741824,i&1073741824,(h&1073741823)+(i&1073741823)];if(g[2]&g[3])return g[4]^2147483648^g[0]^g[1];return g[2]|g[3]?g[4]&1073741824?g[4]^3221225472^g[0]^g[1]:g[4]^1073741824^g[0]^g[1]:g[4]^g[0]^g[1]}function n(h,i,g,j,s,m,o){h=k(h,k(k(i&g|~i&j,s),o));return k(h<<m|h>>>32-m,i)}function p(h,i,g,j,s,m,o){h=k(h,k(k(i&j|g&~j,s),o));return k(h<<m|h>>>32-m,i)}function q(h,i,g,j,s,m,o){h=k(h,k(k(i^g^j,s),o));return k(h<<m|h>>>32-m,i)}function r(h,i,g,j,s,m,o){h=k(h,k(k(g^(i|~j),s),o));return k(h<<m|h>>>32-m,i)}function t(h){var i="",g,j;for(j=0;j<=3;j++){g="0"+(h>>>j*8&255).toString(16);i+=g.substr(g.length-2,2)}return i}var f=[],v,w,x,y,a,b,c,d;e=function(h){var i=h.length,g=0,j=0,s="",m;for(m=0;m<i;m++){var o=h.charCodeAt(m),u=null;if(o<128)j++;else u=o>127&&o<2048?String.fromCharCode(o>>6|192,o&63|128):String.fromCharCode(o>>12|224,o>>6&63|128,o&63|128);if(u!==null){if(j>g)s+=h.substring(g,j);s+=u;g=j=m+1}}if(j>g)s+=h.substring(g,h.length);return s}(e);f=function(h){var i=h.length,g=i+8;g=((g-g%64)/64+1)*16;var j=Array(g-1);for(l=0;l<i;l++)j[(l-l%4)/4]|=h.charCodeAt(l)<<l%4*8;j[(l-l%4)/4]|=128<<l%4*8;j[g-2]=i<<3;j[g-1]=i>>>29;return j}(e);d=271733878;a=1732584193;b=4023233417;c=2562383102;for(e=0;e<f.length;e+=16){v=a;w=b;x=c;y=d;a=n(a,b,c,d,f[e+0],7,3614090360);d=n(d,a,b,c,f[e+1],12,3905402710);c=n(c,d,a,b,f[e+2],17,606105819);b=n(b,c,d,a,f[e+3],22,3250441966);a=n(a,b,c,d,f[e+4],7,4118548399);d=n(d,a,b,c,f[e+5],12,1200080426);c=n(c,d,a,b,f[e+6],17,2821735955);b=n(b,c,d,a,f[e+7],22,4249261313);a=n(a,b,c,d,f[e+8],7,1770035416);d=n(d,a,b,c,f[e+9],12,2336552879);c=n(c,d,a,b,f[e+10],17,4294925233);b=n(b,c,d,a,f[e+11],22,2304563134);a=n(a,b,c,d,f[e+12],7,1804603682);d=n(d,a,b,c,f[e+13],12,4254626195);c=n(c,d,a,b,f[e+14],17,2792965006);b=n(b,c,d,a,f[e+15],22,1236535329);a=p(a,b,c,d,f[e+1],5,4129170786);d=p(d,a,b,c,f[e+6],9,3225465664);c=p(c,d,a,b,f[e+11],14,643717713);b=p(b,c,d,a,f[e+0],20,3921069994);a=p(a,b,c,d,f[e+5],5,3593408605);d=p(d,a,b,c,f[e+10],9,38016083);c=p(c,d,a,b,f[e+15],14,3634488961);b=p(b,c,d,a,f[e+4],20,3889429448);a=p(a,b,c,d,f[e+9],5,568446438);d=p(d,a,b,c,f[e+14],9,3275163606);c=p(c,d,a,b,f[e+3],14,4107603335);b=p(b,c,d,a,f[e+8],20,1163531501);a=p(a,b,c,d,f[e+13],5,2850285829);d=p(d,a,b,c,f[e+2],9,4243563512);c=p(c,d,a,b,f[e+7],14,1735328473);b=p(b,c,d,a,f[e+12],20,2368359562);a=q(a,b,c,d,f[e+5],4,4294588738);d=q(d,a,b,c,f[e+8],11,2272392833);c=q(c,d,a,b,f[e+11],16,1839030562);b=q(b,c,d,a,f[e+14],23,4259657740);a=q(a,b,c,d,f[e+1],4,2763975236);d=q(d,a,b,c,f[e+4],11,1272893353);c=q(c,d,a,b,f[e+7],16,4139469664);b=q(b,c,d,a,f[e+10],23,3200236656);a=q(a,b,c,d,f[e+13],4,681279174);d=q(d,a,b,c,f[e+0],11,3936430074);c=q(c,d,a,b,f[e+3],16,3572445317);b=q(b,c,d,a,f[e+6],23,76029189);a=q(a,b,c,d,f[e+9],4,3654602809);d=q(d,a,b,c,f[e+12],11,3873151461);c=q(c,d,a,b,f[e+15],16,530742520);b=q(b,c,d,a,f[e+2],23,3299628645);a=r(a,b,c,d,f[e+0],6,4096336452);d=r(d,a,b,c,f[e+7],10,1126891415);c=r(c,d,a,b,f[e+14],15,2878612391);b=r(b,c,d,a,f[e+5],21,4237533241);a=r(a,b,c,d,f[e+12],6,1700485571);d=r(d,a,b,c,f[e+3],10,2399980690);c=r(c,d,a,b,f[e+10],15,4293915773);b=r(b,c,d,a,f[e+1],21,2240044497);a=r(a,b,c,d,f[e+8],6,1873313359);d=r(d,a,b,c,f[e+15],10,4264355552);c=r(c,d,a,b,f[e+6],15,2734768916);b=r(b,c,d,a,f[e+13],21,1309151649);a=r(a,b,c,d,f[e+4],6,4149444226);d=r(d,a,b,c,f[e+11],10,3174756917);c=r(c,d,a,b,f[e+2],15,718787259);b=r(b,c,d,a,f[e+9],21,3951481745);a=k(a,v);b=k(b,w);c=k(c,x);d=k(d,y)}return(t(a)+t(b)+t(c)+t(d)).toLowerCase()};
var x = "random text";
alert(md5(x));

2 Comments

Could be a GET rather than a POST, since the result is idempotent. ;-)
@webdev_007: Note that WolfRevoKcats has happily assumed jQuery in the above, although you didn't mention it in your question and jQuery != JavaScript. He's correct that Ajax is easiest with help from a library, though, whether it's jQuery, Prototype, YUI, Closure, or any of several others.
0

You could use an ajax call to your server and have a webservice on the php side returning the information you need. Besides that? I don't think there is a way expect for refreshing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.