PHP 7重新生成具有所需会话ID的会话

时间:2019-01-31 13:12:51

标签: session php-7

我正在运行PHP 7.2,需要重新生成并设置已经激活的会话的所需ID,如下所示...

session_start();        // the session is activated
...
...
$newSessionID = session_create_id();
...
...
if ($someConditionIsSatisfied) {
  session_regenerate_id($newSessionID);
}

问题:session_regenerate_id()自动创建一个新的会话ID,但无法将特定的会话ID作为输入。

...所以我很困惑如何重新生成具有“我想要的ID”的会话。

有人知道我如何在PHP中实现以上目标吗?

1 个答案:

答案 0 :(得分:0)

开始会话之前,您必须设置session_id。

    namespace ViewLibrary
    {
        using System;
        using System.Threading.Tasks;
        using Windows.ApplicationModel.Core;
        using Windows.UI;
        using Windows.UI.Core;
        using Windows.UI.ViewManagement;
        using Windows.UI.Xaml;
        using Windows.UI.Xaml.Controls;
        using Windows.UI.Xaml.Media;

        public static class ViewManagement
        {
            static CoreApplicationView coreView3d;
            static CoreApplicationView coreView2d;

            public static async Task SwitchTo2DViewAsync()
            {
                if (coreView3d == null)
                {
                    coreView3d = CoreApplication.MainView;
                }
                if (coreView2d == null)
                {
                    coreView2d = CoreApplication.CreateNewView();

                    await RunOnDispatcherAsync(
                        coreView2d, 
                        async () =>
                        {
                            Window.Current.Content = Create2dUI();
                        });
                }
                await RunOnDispatcherAsync(coreView2d, SwitchViewsAsync);
            }

            static UIElement Create2dUI()
            {
                var button = new Button()
                {
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment   = VerticalAlignment.Stretch,
                    Content             = "Back to 3D",
                    Background          = new SolidColorBrush(Colors.Red)
                };

                button.Click += async (s, e) =>
                {
                    await SwitchTo3DViewAsync();
                };

            return (button);
        }

        static async Task RunOnDispatcherAsync(CoreApplicationView view,Func<Task> action)
        {
            await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
        }

        public static async Task SwitchTo3DViewAsync()
        {
            await RunOnDispatcherAsync(coreView3d, SwitchViewsAsync);
        }

        static async Task SwitchViewsAsync()
        {
            var view = ApplicationView.GetForCurrentView();
            await ApplicationViewSwitcher.SwitchAsync(view.Id);
            Window.Current.Activate();
        }
    }
}

您的问题已在这里得到解答:How to get/set session_id() or should it be generated automatically?