开发者

Store JavaScript Result In Php variable

I have Some Passwords which contain some special and methimatical chracters like this password "HHE1134ƒ" if i want to generate password with php md5() then this not give me correct hashes like this "679b6dc6122a9c83ed31476ee82af36e".

So i have Java Script which can generate correct hash for my passwords But how can i store this value to mysql

my code is given below please Help Me.

$result = mysql_query("SELECT * FROM `table`") or die(mys开发者_运维问答ql_error());

while ($row = mysql_fetch_assoc($result))
{

$user_id = $row['user_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$spno = $row['spno'];
$password = $row['password'];

$tmp =  "<script type='text/javascript'> document.write(MD5('".$password."'));</script>";

$query1="UPDATE `wp_110504users` SET `user_pass` = '$tmp' WHERE `user_login` = '$user_id'";

mysql_query($query1) or die(mysql_error());
}

this will give me error like

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'text/javascript'> document.write(MD5('HW72'));' WHERE `user_login` = '0' at line 1

?>

Some one tell me use Ajax, Please tell me how can i use Ajax for this code can you give me an example .....


You cannot run JavaScript code inside your PHP code on the server. The PHP interpreter only interprets PHP code.

You don't need to, both MySQL and PHP have md5 functions to compute those hashes as well.


Javascript doesn't run until it reaches the user's browser. This is called client side.

PHP is server side. All your PHP code executes before the web page is delivered to the user. Then your Javascript is executed on their own computer, by their web browser.

The only way to send something from Javascript to the server is using Ajax. You could look into it, but I don't think you should at your current experience level with the two languages. How long have you been using them for?

You can just use the md5() function which is built right into php btw: http://php.net/manual/en/function.md5.php


Use php native md5 function:

md5($password);


PHP is interpreted before javascript reaches the browser. You cannot do this that way. Read more about AJAX, this will help you.

BTW: why are you generating md5 hashes with javascript?


This is very normal, you userpass value will just be <script type.... Javascript is a client side scripting language, whereas php runs server side, how would you want your md5 to be interpreted server-side?

What you can do, is hash the password before sending it, but it would make you trust the visitor, what you should never do, or simply use php function for md5 hashing.


I think you copy-pasted code not correctly. According to error, you trying to put $userpass variable in SQL statement, but, in code, you are using $tmp variable, which is not defined at all.


Try this:

$tmp = md5($password);
$query1="UPDATE `wp_110504users` SET `user_pass` = '$tmp' WHERE `user_login` = '$user_id'";

Also u can use MySQL function:

$query1="UPDATE `wp_110504users` SET `user_pass` = MD5('$tmp') WHERE `user_login` = '$user_id'";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜