用户输入验证,客户端还是服务器端? [PHP/JS]

用户输入验证,客户端还是服务器端? [PHP/JS]

问题描述:

在使用JS将用户输入发送到服务器或使用PHP将其发送到服务器端之前,验证用户输入是否更好?还是只是为了安全起见才值得做这两者?

Is it better to validate user input before it's sent to the server with JS or server side with PHP? Or maybe it's worth doing both just to be on the safe side?

我正在创建一个站点(目前非常简单),该站点具有成员区域/管理区域/等.目前,我只有用户名和密码作为用户输入,将来还会有更多(电子邮件,地址等),但是检查数据的最佳做法是什么?

I'm creating a site (very simple at the moment) that has a members area/admin area/etc. At the moment i only have user input of Username and Password, in the future there will be more (email, address, etc), but whats the best practice of checking the data?

在用户正确之前,我是否向其中抛出"if ... else"语句?或者,对于用户输入的每个值,可能有单独的变量,如果正确或错误,则将其设置为true或false? (例如电子邮件验证,以确保其为电子邮件格式)

Do i throw a load of 'if...else' statements at it until the user gets it right? Or maybe have separate variables for each value entered by the user and set it to true or false if it's correct or wrong? (like e-mail validation to make sure it's in an email format)

有很多方法可以做到,但是您会建议哪些呢?当我可以用10行完成这项工作时,我不想写50行代码...如果可以的话:p

There are a lot of ways to do it, but which ones you would suggest? I don't want to be writing 50 lines of code when i could do the job in 10 lines...if that makes sense :p

任何帮助将不胜感激,谢谢! :)

Any help would be appreciated, thanks! :)

服务器端验证是必须,客户端验证是 plus .

Server-side validation is a must, client-side validation is a plus.

如果仅使用客户端验证,那么邪恶的人会入侵您的系统以发布未经验证的内容-破坏脚本并可能利用您的系统.从安全角度来看,这非常糟糕.

If you only use client-side validation, nefarious people will hack your system to post un-validated stuff - breaking your scripts, and potentially exploiting your system. This is very bad from a security standpoint.

也就是说,您还应该包括客户端验证,因为这比往返服务器的速度要快得多,并且可以为您的用户提供即时反馈.这样可以使您的用户满意,并使他们回到您的网站.

That said, you should also include client-side validation, since that's much quicker than a round trip to the server, and gives your users instant feedback. This'll keep your users happy, and will have them coming back to your site.

因此,如果可能,请同时使用.如果您不能/不愿意,那么至少可以在服务器端进行.仅客户端验证是灾难的根源!

So, if possible, use both. If you can't/won't, then at least do it server-side. Client-side-only validation is a recipe for disaster!