如何为 PHP 5.4+ 替换 session_register()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18859750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:19:44  来源:igfitidea点击:

How to replace session_register() for PHP 5.4+

phpsession

提问by Michael Curving

out of the blue I was getting the following error when logging in to one of my sites:

登录到我的网站之一时,我突然收到以下错误:

Call to undefined function session_register()

调用未定义的函数 session_register()

After some research I saw that session_register() is deprecated after PHP 5.4. I checked in my hosting control panel, and sure enough it says current version I'm running is PHP 5.4.19. I assume they just forced an upgrade which is why this problem seemed to occur out of the blue.

经过一番研究,我发现 session_register() 在 PHP 5.4 之后已被弃用。我检查了我的主机控制面板,果然它说我正在运行的当前版本是 PHP 5.4.19。我认为他们只是强制升级,这就是为什么这个问题似乎是突然发生的。

I looked through several Stack Overflow posts and reviewed links to PHP documentation. From what I gather, session_register() is deprecated and should be replaced with $_SESSION instead.

我浏览了几篇 Stack Overflow 帖子,并查看了 PHP 文档的链接。据我所知, session_register() 已弃用,应替换为 $_SESSION 。

Problem is, both of those already appear in my code. Here is the code in my index.php file:

问题是,这两个都已经出现在我的代码中。这是我的 index.php 文件中的代码:

  53 <? if ($username) {
  54    session_register("ADMIN");
  55    $_SESSION['ADMIN'] = $username;
  56 ?>

So I hoped just by removing line 54 (the deprecated piece) then it should work, however that is not the case. It broke the code when I did that.

所以我希望通过删除第 54 行(已弃用的部分)然后它应该可以工作,但事实并非如此。当我这样做时,它破坏了代码。

Based on other posts I read HEREand HEREthey seem to be saying that the code I see in line 55 above should by itself do the trick. So I'm a bit confused why it didn't work. If anyone can tell me what code I should use I would sincerely appreciate it. The developer of the script is not really offering support.

根据我在此处此处阅读的其他帖子,他们似乎在说我在上面第 55 行中看到的代码本身应该可以解决问题。所以我有点困惑为什么它不起作用。如果有人能告诉我应该使用什么代码,我将不胜感激。脚本的开发者并没有真正提供支持。

NOTE: Also, yes there is session_start() called at the very top of the page.

注意:另外,是的,在页面的最顶部调用了 session_start()。

回答by krolow

Possible it is missing the call for session_start, session_registermake this automatically, so what you have to do is just make sure that you have called session_start before use $_SESSIONglobal.

可能缺少对session_start的调用,session_register会自动进行此调用,因此您要做的就是确保在使用$_SESSION全局之前已调用 session_start 。

You can check more here http://www.php.net/manual/en/function.session-register.php

你可以在这里查看更多http://www.php.net/manual/en/function.session-register.php

If session_start() was not called before this function is called, an implicit call to session_start() with no parameters will be made. $_SESSION does not mimic this behavior and requires session_start() before use.

如果在调用此函数之前未调用 session_start(),则将进行不带参数的对 session_start() 的隐式调用。$_SESSION 不会模仿这种行为,并且在使用前需要 session_start()。

回答by 01e

Because session_register('ADMIN')registers the global variable with that name in the current session, so maybe in your code you have $ADMIN variable, find them and replace them with:

因为session_register('ADMIN')在当前会话中使用该名称注册全局变量,所以可能在您的代码中您有 $ADMIN 变量,找到它们并将它们替换为:

$_SESSION['ADMIN']

I hope this will help you.

我希望这能帮到您。

回答by Michael Curving

Looks like I overlooked the obvious. I was on the right track by simply removing the old session_register code.

看起来我忽略了显而易见的事情。通过简单地删除旧的 session_register 代码,我走上了正确的轨道。

But notice the original code I posted starts

但是请注意我发布的原始代码开始

<?

instead of

代替

<?php

doh

回答by Starx

Add a session_start()at the begining of your code and $_SESSIONto manipulate it or create it.

session_start()在代码的开头添加 a并对其$_SESSION进行操作或创建。