简体   繁体   English

从href链接传递变量的最佳方法

[英]Best idea to pass variables from a href link

I have a href link, and I would like to pass some variables without using them in a link, what is the best idea? 我有一个href链接,我想传递一些变量而不在链接中使用它们,最好的主意是什么?

<a href="Files/download.php?file=fileName">Click</a>

I would like to get these variables into my download.php page to update them into my database table: 我想将这些变量添加到我的download.php页面中,以将它们更新到我的数据库表中:

timestamp
downloads
hits
views

Thanks in advance 提前致谢

<a href="Files/download.php?file=fileName" >Click</a>

Its all depend on your situation how to do it because there are lots of ways to do this 这完全取决于您的情况,因为这样做的方法很多

1) if you dont want to show the parameters to user, then you can encrypt them & add them in URL. 1)如果您不想向用户显示参数,则可以对其进行加密并将其添加到URL中。 2) if you dont want parameters in URL, then you can create hidden input tags for all parameters & then using POST method submit that form on HREF click or store the values in session 2)如果您不希望在URL中使用参数,则可以为所有参数创建隐藏的输入标签,然后使用POST方法在HREF上提交该表单,或者将其存储在会话中

<form id='hidden_form' action='Files/download.php'>
<input type='hidden' name='timestamp' value='some value' >
<input type='hidden' name='downloads' value='some value' >
<input type='hidden' name='hits' value='some value' >
<input type='hidden' name='views' value='some value' >

$_SESSION['timestamp']  = '<some value>';
$_SESSION['downloads']  = '<some value>';
$_SESSION['hits']   = '<some value>';
$_SESSION['views']  = '<some value>';

Using a link, you don't have much flexibility. 使用链接,您没有太多的灵活性。 You can really only pass them through the url. 您实际上只能通过URL传递它们。

You could do more however if this was a form, and these variables were passed as POST data. 但是,如果这是一种形式,则可以做更多的事情,并且这些变量作为POST数据传递。 Then they would be hidden to the user and not present in the url. 然后它们将对用户隐藏,而不显示在url中。 You could use hidden inputs: 您可以使用隐藏的输入:

<input type='hidden' name='myVariable' value='5' />

You could also store them in a $_SESSION but it's difficult to say what's best for your situation without context. 您也可以将它们存储在$_SESSION但是在没有上下文的情况下很难说出最适合您的情况。

如果要使用href链接,请使用以下内容。

<a href="Files/download.php?file=fileName&timestamp=value&downloads=value&hits=value&views=value">Click</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM