如何在没有表单字段的情况下从表单传递数据? (PHP)

发布于 2024-11-03 07:18:47 字数 191 浏览 0 评论 0原文

我有一个用于编辑用户名和电子邮件的表单。 因此,当它更新姓名和电子邮件时,它需要用户名来确定应该更新哪一行。

所以我想知道是否有任何元素随表单一起传递,但没有显示值或在输入标记中可编辑。

所以我从一个脚本中获取用户名。 编辑用户脚本从数据库中获取指定用户名的姓名和电子邮件。 然后它将新名称和电子邮件以及用户名传递给另一个脚本来更新它。

I have a form for editing a users name and email.
So when it updates the name and email, it needs the username to identify which row it should update.

So i wanted to know if there is any element which is passed with the form but without showing the value or being editable in the input tag.

So i get the username from one script.
The edit user script gets the name and email from the database with the specified username.
Then it passes that new name and email with the username to another script which updates it.

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

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

发布评论

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

评论(8

╭ゆ眷念 2024-11-10 07:18:47

我相信您正在寻找

 <input type='hidden' name='username' value='theusername' />

隐藏 - 只能在 HTML 文档的源代码中看到
name - 提交时 $_REQUEST/$_POST/$_GET($_POST 或 $_GET 取决于您提交表单的方式)变量中的位置
value - 您希望此表单关联的用户名

专业提示:有办法知道谁在尝试更新用户,这样您就不会被未经授权的人更新您的用户信息。对于某人来说,更改表单中的用户名并尝试更新其他人是非常容易的。

I believe you are looking for

 <input type='hidden' name='username' value='theusername' />

hidden - can only be seen in the source of your HTML document
name - where it will be in the $_REQUEST/$_POST/$_GET ($_POST or $_GET depending on how you are submitting your form) variable on submit
value - the username you want this form to relate to

PRO TIP: Have a way to tell who is trying to update users so you don't have unauthorized people updating your user information. It would be very easy for someone to change the username in the form and try to update someone else.

本宫微胖 2024-11-10 07:18:47

您可以使用隐藏的输入类型

<input type="hidden" name = "username" value="<?php echo $username ?>">

You can use input type hidden

<input type="hidden" name = "username" value="<?php echo $username ?>">
南城旧梦 2024-11-10 07:18:47

使用:

 <input type="hidden" />

HIDDENFORMINPUT 元素的 TYPE 属性值。它指示在文档中不可见并且用户不与之交互的表单字段。它可用于传输有关客户端或服务器的状态信息。隐藏字段通常存储默认值(例如通过 php),或者通过 JavaScript 更改其值。

更多信息

use an:

 <input type="hidden" />

HIDDEN is a TYPE attribute value to the INPUT element for FORMs. It indicates a form field that does not appear visibly in the document and that the user does not interact with. It can be used to transmit state information about the client or server. Hidden fields often store a default value (e.g.via php), or have their value changed by a JavaScript.

more here

后eg是否自 2024-11-10 07:18:47

使用隐藏的输入标签:

<input type='hidden' name='username' value='theusername' />

Use a hidden input tag:

<input type='hidden' name='username' value='theusername' />
老娘不死你永远是小三 2024-11-10 07:18:47

正如所有其他人所说,您需要隐藏的输入。虽然它是可编辑的,但永远不要相信它,因为你永远不相信来自外部的任何其他数据。

但我想补充一点,最好不要使用用户名来标识行,而是将 ID 列作为主键添加到数据库中(可能会自动递增),然后在表单中使用它。

像这样的东西

<input type="hidden" name="userid" value="<?=$userid?>" />

As all the others stated you need a hidden input. It WILL be editable though, never trust it as you never trust any other data coming from outside.

But I'd like to add that it would be nicer not to use the username for identifying a row, add an ID column as a primary key instead to your database (possibly auto incremented), and use that in your form.

Something like

<input type="hidden" name="userid" value="<?=$userid?>" />
浪漫之都 2024-11-10 07:18:47

Arun,您可以使用 GET 将变量从一个页面传递到另一个页面。只需将 URL 构造为 edituser.php?username=arun 等即可。这是将变量或数据(当然除了 Cookie 之外)传递到不使用表单标签的其他页面的唯一可能方法。
第二种方法是使用 JavaScript 创建隐藏表单字段并使用用户名更新它。
第三种是简单地添加隐藏的输入标签。但这和后者将需要表单标签。

需要注意的是,过滤用户输入,可以是 JS、GET 或隐藏字段。

Arun, you can use GET to pass variables from one page to another page. Simply construct URLs as edituser.php?username=arun and so on. This is the only possible way to pass on variables or data, of course apart from cookies, to other pages w/out using form tags.
Second method is to use JavaScript to create a hidden form field and update it with username.
Third one is to simply add hidden input tags. But this and latter will require form tags.

A word of caution, filter user inputs, be JS, GET or hidden fields.

云柯 2024-11-10 07:18:47

您可以使用隐藏的表单字段:

<input type="hidden" name="originalUsername" value="something" />

这不会在浏览器中的表单上呈现,并且可能会被用户忽略和忽视。

但是,请注意此是可编辑的。 不要依赖此作为安全措施。提交表单后,请确保提交表单的用户(使用您现有的任何身份验证和授权机制)有权进行此更改,然后再将其保存到数据库。任何正在提交的表单字段都可以编辑。

You can use a hidden form field:

<input type="hidden" name="originalUsername" value="something" />

This won't render on the form in the browser and will likely be ignored and unnoticed by the user.

However, be aware that this is editable. Do not rely on this as a security measure. When the form is submitted, make sure that the user submitting the form (using whatever authentication and authorization mechanisms you have in place) is authorized to make this change before persisting it to the database. Any form field being submitted can be edited.

紧拥背影 2024-11-10 07:18:47

如果你想安全地使用它,请使用它:

<input type='hidden' name='username' value='<?php echo encode("Please Encode Me!","This is a key"); ?>' />

这将导致:

<input type='hidden' name='username' value='p3e4e4241674d2r4m4i5o464a4f2p3k5c2' />

并且在修改脚本中你必须使用:

<?php $username = decode("p3e4e4241674d2r4m4i5o464a4f2p3k5c2","This is a key"); ?>

下面有用于编码/解码的 PHP 函数:

<?php

function encode($string,$key) {
    $key = sha1($key);
    $strLen = strlen($string);
    $keyLen = strlen($key);
    for ($i = 0; $i < $strLen; $i++) {
        $ordStr = ord(substr($string,$i,1));
        if ($j == $keyLen) { $j = 0; }
        $ordKey = ord(substr($key,$j,1));
        $j++;
        $hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
    }
    return $hash;
}

function decode($string,$key) {
    $key = sha1($key);
    $strLen = strlen($string);
    $keyLen = strlen($key);
    for ($i = 0; $i < $strLen; $i+=2) {
        $ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
        if ($j == $keyLen) { $j = 0; }
        $ordKey = ord(substr($key,$j,1));
        $j++;
        $hash .= chr($ordStr - $ordKey);
    }
    return $hash;
}

?>

Use this if you want to use it safely:

<input type='hidden' name='username' value='<?php echo encode("Please Encode Me!","This is a key"); ?>' />

wich will result into:

<input type='hidden' name='username' value='p3e4e4241674d2r4m4i5o464a4f2p3k5c2' />

and in the modification script you will have to use:

<?php $username = decode("p3e4e4241674d2r4m4i5o464a4f2p3k5c2","This is a key"); ?>

Below you have the PHP functions for the ENCODE/DECODE:

<?php

function encode($string,$key) {
    $key = sha1($key);
    $strLen = strlen($string);
    $keyLen = strlen($key);
    for ($i = 0; $i < $strLen; $i++) {
        $ordStr = ord(substr($string,$i,1));
        if ($j == $keyLen) { $j = 0; }
        $ordKey = ord(substr($key,$j,1));
        $j++;
        $hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
    }
    return $hash;
}

function decode($string,$key) {
    $key = sha1($key);
    $strLen = strlen($string);
    $keyLen = strlen($key);
    for ($i = 0; $i < $strLen; $i+=2) {
        $ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
        if ($j == $keyLen) { $j = 0; }
        $ordKey = ord(substr($key,$j,1));
        $j++;
        $hash .= chr($ordStr - $ordKey);
    }
    return $hash;
}

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