在 CodeIgniter 中将 Isset 与本机会话结合使用

发布于 2024-11-14 07:02:33 字数 600 浏览 4 评论 0原文

我读过有关 CI 如何以不同于本机会话的方式处理会话的内容,并且对于将所有数据存储在 cookie(?) 中感到有点不安全,这与仅存储会话 ID(?) 的 PHP 本机会话不同。因此,我决定使用本机会话,而不使用 CI native_session 库。

现在,我知道 CI 中的输入类使用如下的真/假语句来验证 Isset:

if ($this->input->post('something'))

这使得 Isset 函数无法工作(它给出一个错误)。但是,我想使用 Isset 函数检查我的本机会话,那么我该如何执行此操作?我尝试过

if (isset($_SESSION['keyHere']))

这给了我一个错误。

总结一下:我想在会话数组上使用 Isset,因为我觉得使用

if ($_SESSION['keyHere'])

而不使用 Isset 可能会“危险/愚蠢”。

另外,作为最后一个问题,我很好奇您认为哪种会话处理最安全? CI 的会话类或 PHP 的服务器端存储等本机处理?我非常希望在会话方面感到尽可能安全,无论这是否意味着我必须编写更长的代码。

I've read about how CI handles sessions differently than native sessions and feel slightly insecure about storing all the data in a cookie(?), unlike PHP's native session which only stores the session ID(?). So I've decided to use native sessions without the CI native_session library.

Now, I know that the Input Class in CI validates Isset with a true/false statement like this:

if ($this->input->post('something'))

which renders the Isset function unable to work (it gives an error). However, I'd like to check my native sessions with the Isset function, so how can I do this? I've tried

if (isset($_SESSION['keyHere']))

which gives me an error.

So to sum up: I want to use Isset on my session array as I feel using

if ($_SESSION['keyHere'])

without the Isset might be 'dangerous/foolish'.

Also, as a last question I'm curious about which session handling you think is safest? CI's Session Class or PHP's native handling with server-side storage etc.? I'd very much like to feel as safe as possible when it comes to sessions, no matter if it means I'll have to write longer code.

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

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

发布评论

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

评论(7

‘画卷フ 2024-11-21 07:02:33

好的,我们来谈谈您在 isset 中遇到的问题!
我同意后面的所有答案,空函数是一个很好的尝试,也使用 array_key_exists,但是 isset 在 php 语言上有自己的位置。

起初,您没有发布您收到的错误,但正如 @GolezTrol 所说,您可能在 session_start() 方面遇到问题。

您需要将其放在页面顶部、任何脚本之前。我会将此代码放在所有脚本之前,最好将其放在 config/config.php 页面内,在其开头,或者可能在根index.php

使用它,您将在所有脚本上启动会话您系统的页面。
嗯,关于你的第二个问题。

我认为 PHP 原生的会话非常安全,但 Codeigniter 的会话也非常好。

Codeigniter 存在一些安全问题,当我们谈论 cookie 时,如果我们正在处理系统的重要数据,则很难使用它,特别是如果其他人可以编辑它,考虑一下我上面描述的所有内容,我可以说 Codeigniter 有一个很好的会话库,即使我们正在处理重要的数据。

如果你不相信cookie的安全性,就直接放在数据库里,就不会有任何麻烦了。好处是能够在系统的每个地方使用此类进行工作!

实际上我正在使用数据库的方式,我建议它。

Ok so, lets talk about the problem that you're having with isset!
i agree to all answers behind, the empty function its an good try, using array_key_exists too, but isset have your own place on php language.

At first you didn't post the error taht you're getting, bu as @GolezTrol saidyou're probally having trouble with session_start().

You need to put it on the top of the page, before any scripts. I would put this code before all scripts, and the best place to put it its inside the config/config.php page, at the start of it, or maybe on the root index.php

Using it, you will starting the session on all pages of your system.
Well, about your second question.

I think the session native of PHP its really secure, but the session of Codeigniter it's really nice too.

Codeigniter has some problems with the security, when we talk about cookies, if we are handling with important data of system, its hard to work with it, especially if someone else could edit it, thinking about all of it that i describe above, i could say that Codeigniter has a good seession library, even if we are working with important data.

If you don't believe on the security of cookie, just put on database, and you will have any trouble with it. The good thing its the ability and the facility to work with this class on everyplace of the system!

Actually i'm using the way of database, and i suggest it.

分开我的手 2024-11-21 07:02:33

我会使用array_key_exists而不是issetisset 还会检查值是否不为 null,因此具有现有键但值为 null 的数组仍将返回 false。您知道它是一个数组,并且想要检查某个键是否存在,因此使用 array_key_exists 是最有意义的。

但这不是问题。 :D

我认为你需要先调用 session_start()

只要您在单个 Web 服务器上,PHP 的本机会话处理就可以很好地执行。

I would use array_key_exists instead of isset. isset also checks if the value is not null, so an array with an existing key but a value of null will still return false. You know it is an array and you want to check if a key exists, so it makes the most sense to use array_key_exists.

But that's not the problem. :D

I think you need to call session_start() first.

PHP's native session handling performs just fine as long as you are on a single webserver.

时光礼记 2024-11-21 07:02:33
$data=$this->session->userdata("session variable");

if($data)
{
  // do something
}

在 CI 代码文件中使用它。它一直对我有用!

$data=$this->session->userdata("session variable");

if($data)
{
  // do something
}

use this in your CI code files. it works for me all the time !

戏舞 2024-11-21 07:02:33
$logindata = $this->session->userdata('username');

if ($logindata !== FALSE)
{
  //it exists
}

可能有帮助!

$logindata = $this->session->userdata('username');

if ($logindata !== FALSE)
{
  //it exists
}

might help!

久伴你 2024-11-21 07:02:33

我一直使用 php 函数 empty(); 但如果您不知道自己在做什么,它的工作方式会有所不同。

  • 如果它是一个空数组,它将
    return FALSE
  • 如果它是 NULL,它将返回 FALSE

我个人对新的 session 类没有什么问题 在 CI 2.0.2 中。创建一个名为“sessions”的表并将会话存储在数据库中,并将 sess_use_database 设置为 true。还将 sess_encrypt_cookies 设置为 true,最后,如果您希望会话与用户 IP 匹配,请使用 sess_match_ip。哦,一定要设置加密密钥,这将使它更加安全。

PHP 会话很好,但我个人更喜欢 CI 会话,因为它提供了更大的灵活性。特别是当您使用负载均衡器运行多个网络头时。

希望有帮助!

I've always used php function empty(); though it works differently if you don't know what you are doing.

  • If it's an empty array, it will
    return FALSE
  • If it's NULL, it will return FALSE

I personally had little problems with the new session class in CI 2.0.2. Create a table called "sessions" and store the sessions in the database, using sess_use_database to true. Also set sess_encrypt_cookies to true, and lastly, if you want sessions to match with user IPs, use sess_match_ip. Oh, and make sure to set encryption key, which will make it even more secure.

PHP sessions are nice, but I personally like CI sessions better because it gives more flexibility. Especially when you are running multiple web heads with load balancer.

Hope that helps!

我很OK 2024-11-21 07:02:33

为什么不使用内置的 CodeIgniter 会话类?它将提供与输入类相同的功能,但不会强迫您使用isset()。

它允许加密会话并使用它自己的会话实现。

$this->session->userdata("keyHere");  // returns false if not set

Why not use the built in CodeIgniter Session Class? it will provide the same functionality as the input class in regards to not forcing you to use isset().

It allows for encrypted sessions and uses it's own session implementation.

$this->session->userdata("keyHere");  // returns false if not set
层林尽染 2024-11-21 07:02:33

您确实应该使用 codeigniter 会话类。您可以使其将会话数据存储在数据库中,以便 cookie 仅保留唯一标识符。您还可以将该 cookie 设置为使用 codeigniter 安全密钥进行加密。

然后,当您访问会话信息时,它的行为就像正常的输入方法一样,因此您可以只执行 if($this->session->userdata('name')) 而不是使用 isset 或empty 或其他。

You really should use the codeigniter session class. You can make it store the session data in the db so the cookie holds just a unique identifier. You can also set that cookie to be encrypted with the codeigniter security key.

Then, when you access session information, it acts like the normal input methods and so you can just do if($this->session->userdata('name')) rather than using isset or empty or whatever.

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