使用 $_GET 传递会员 ID

发布于 2024-12-08 17:51:38 字数 795 浏览 3 评论 0原文

我在尝试创建会员登录系统时遇到问题,其中每个用户都被定向到他们自己的可编辑配置文件。我现在处于登录阶段,并尝试将用户的 ID 从登录页面安全地传递到他们的个人资料页面,以便我可以显示他们的个人资料信息。

现在,在此之后我被重定向到“mydomain.com/index.php?test=Resource%20id%20#8”(我想要,例如“mydomain.com/index.php?test=1111”)代码块:

$user_id = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$user' AND `active`='1'");  
header("location: profile.php?test='$user_id'");
exit();

其中$user 已使用trim、htmlspecialchars 和mysql_real_escape_string 进行清理。用户到达“profile.php”后,我计划在弄清楚如何正确传递用户 ID 后实现类似的操作:

$user_id=(int)$_GET['$user_id'];
mysql_real_escape_string($user_id);
if (isset($user_id)) {
   $user_id = preg_replace('#[^0-9]#i', '', $user_id); // filter everything but numbers

代码还有更多内容,但我相信我已经包含了所有相关信息。任何建议、智慧、想法和评论总是非常感激。非常感谢您的支持。

Im having a problem when trying to create a member login system where each user is directed to their own editable profile. I'm at the login stage right now, and trying to pass the user's id securely from the login page to their profile page so that I can display their profile information.

Right now, I am being redirected to 'mydomain.com/index.php?test=Resource%20id%20#8', (I want, for example 'mydomain.com/index.php?test=1111') after this chunk of code:

$user_id = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$user' AND `active`='1'");  
header("location: profile.php?test='$user_id'");
exit();

where $user has been sanitized using trim, htmlspecialchars, and mysql_real_escape_string. After the user arrives at 'profile.php,' I am planning on implementing something like this after I can figure out how to properly pass the user id:

$user_id=(int)$_GET['$user_id'];
mysql_real_escape_string($user_id);
if (isset($user_id)) {
   $user_id = preg_replace('#[^0-9]#i', '', $user_id); // filter everything but numbers

There is more to the code, but I believe I have included all relevant information. Any advice, wisdom, thoughts, and comments are always very much appreciated. Thank you very much for your support.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断肠人 2024-12-15 17:51:38

您无法立即查询并得到结果。

尝试像这样修复上面的部分。

$user_id = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$user' AND `active`='1'");
$user_id = mysql_result($user_id, 0, 'id');  
header("location: profile.php?test='$user_id'");
exit();

You cannot query and get the result right away.

Try to fix above part as like this.

$user_id = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$user' AND `active`='1'");
$user_id = mysql_result($user_id, 0, 'id');  
header("location: profile.php?test='$user_id'");
exit();
贪了杯 2024-12-15 17:51:38

你的代码太糟糕了。

  1. isset 毫无意义,$user_id 始终设置在那里。
  2. preg_replace 是没有意义的 - 在 (int) 之后,变量中只有数字
  3. mysql_real_escape_string 是没有意义的 - 在 (int) 之后不会有坏字符

所以用单行替换整个块

$user_id = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 0;

Your code is terrible.

  1. isset is pointless, $user_id is always set there.
  2. preg_replace is pointless - after (int) only numbers will be in variable
  3. mysql_real_escape_string is pointless - after (int) there will be no bad characters

So replace that whole block with single line

$user_id = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文