不使用 $_GET 或 URI 段传递变量 - CodeIgniter
我有一个博客,我希望用户单击链接即可反转配色方案(从深色浅色到浅色深色)。该链接将在博客的每个页面上提供。
如果我没有使用 CodeIgniter,我将创建一个带有参数的链接(例如“index.php?inverted=true”),并在我的控制器中检测是否设置了 $_GET['inverted'],设置 $_SESSION 变量。当设置 $_SESSION 时,链接将被更改,并且将添加额外的 CSS 文件。该链接的新 href 类似于“index.php?inverted=false”,并且 $_SESSION 变量将被取消设置。它将在站点范围内工作,而不管 URI 的其余部分如何。
我尝试过在 CodeIgniter 中使用 uri_segement 函数,但是因为这需要在整个站点上进行,所以我的 URI 发生了变化(有时只是“index.php/controller/function”,其他时候是“index.php/controller/function/para1”) /para2/para3”)。因此我不能只创建链接: [href="$_SESSION['REQUEST_URI'] . "/inverted"] 然后检测该 URI 段是否存在,因为我不知道“inverted”段是哪个数字另外,如果用户在同一页面上并单击链接两次,则 URI 会不断添加“/inverted”,
因此我可以使用 $_POST 数组来代替,但我知道这一点。没有好的做法
吗?
I have a blog where I want the user to click a link to be able to invert the colour scheme (from dark on light to light on dark). This link will be available on each page of the blog.
If I wasn't using CodeIgniter, I would create a link with a parameter (e.g. "index.php?inverted=true") and in my controller detect if $_GET['inverted'] was set, set a $_SESSION variable. The link would be changed and the additional CSS file would be added when $_SESSION is set. The link's new href would be something like "index.php?inverted=false" and the $_SESSION variable would be unset. It would work site-wide and regardless of the rest of the URI.
I've tried using the uri_segement functions in CodeIgniter, but because this needs to be across the whole site, my URIs change (sometimes just "index.php/controller/function", other times "index.php/controller/function/para1/para2/para3"). Therefore I can't just make my link: [href="$_SESSION['REQUEST_URI'] . "/inverted"] and then detect if that URI segment exists because I don't know which number the segment "inverted" would be. Also, if a user is on the same page and clicks the link twice, the URI keeps adding "/inverted" again and again.
I made a mock form so I could use the $_POST array instead, but I know this isn't good practice.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让链接按照您的描述通过 POST 提交表单,然后设置 SESSION 变量,之后发出重定向到引用页面 (HTTP_REFERER)。您可以执行类似的操作来关闭此选项。
Make the link submit a form via POST as you described, then set the SESSION variable, after that, issue a redirect to the referring page (HTTP_REFERER). You can do something similar to toggle this option off.