Cookie与会话|我得到了不同的结果

时间:2016-12-01 00:49:15

标签: php session cookies session-cookies

我想要实现的目标,

  1. 用户输入one_page.php和我们require('form.php'),以便用户在$_SESSION['foo']未设置时进行填充。
  2. 用户提交表单并设置$_SESSION['foo']
  3. 我们header ('Location: one_page.php')(实际上就像重装)
  4. 我们进入if我们unset($_SESSION['foo'])require('something_else.php')
  5. 如果用户重新加载site.php或重新输入,则他将再次获得form.php
  6. 我不会向您展示实际的代码,因为它太大而且我不想只粘贴它的一部分,但我在下面的两个例子中重现了这个问题。

    使用cookies代码完全按预期运行。

    使用session,就像我们if unset($_SESSION['foo']) if一样,然后我们离开else并进入page1.php。< / p>

    当我在例如page2.php设置会话并将用户重定向到<?php if (isset($_POST['submit'])) { setcookie('foo', 'foo', time() +3600); header('Location: one_page.php'); } if (isset($_COOKIE['foo'])) { setcookie('foo', 'foo', time() -3600); echo "We entered the IF"; //require('something_else.php') } else { echo "We entered the ELSE"; //require('form.php') } ?> <!-- The form which is required in my case --> <html> <body> <form method="post"> <button name="submit">Button</button> </form> </body> </html 以取消会话时,一切似乎都很好。当我在我取消设置的同一页面上创建会话时,我无法使其正常工作。

    实施例

    使用Cookie 当我们按下按钮时我们进入IF

    <?php
        session_start();
        if (isset($_POST['submit'])) {
            $_SESSION['foo'] = "foo";   
            header('Location: one_page.php');
        }
    
        if (isset($_SESSION['foo'])) {
            unset($_SESSION['foo']);
            echo "We entered the IF";    //require('something_else.php')
        } else {
            echo "We entered the ELSE";  //require('form.php')
        }
    ?>
    <!-- The form which is required in my case -->
    <html>
        <body>
            <form method="post">
                <button name="submit">Button</button>
            </form>
        </body>
    </html>
    

    使用会话 无论是什么

    ,我们都在ELSE中
     java.lang.NoClassDefFoundError: Failed resolution of: Lcom/externalibrary/R$string
    
    
    11-30 17:06:02.475 22610-22610/com.mycompany.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.mycompany.myapp, PID: 22610
                                                                                 java.lang.NoClassDefFoundError: Failed resolution of: Lcom/externalibrary/R$string;
                                                                                     at com.externalibrary.client.externalibrarySettingsManager.getShouldAnnotateBlobs(externalibrarySettingsManager.java:80)
                                                                                     at com.externalibrary.client.externalibrarySettingsManager.getMotionDetectorConfigurationFromPreference(externalibrarySettingsManager.java:39)
                                                                                     at com.externalibrary.client.externalibrarySettingsManager.getConfigurationFromPreference(externalibrarySettingsManager.java:28)
                                                                                     at com.externalibrary.client.externalibraryClient.<init>(externalibraryClient.java:24)
                                                                                     at com.mycompany.myapp.MainActivity.onCreate(MainActivity.java:191)
                                                                                     at android.app.Activity.performCreate(Activity.java:6664)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                                  Caused by: java.lang.ClassNotFoundException: Didn't find class "com.externalibrary.R$string" on path: DexPathList[[zip file "/data/app/com.mycompany.myapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.myapp-2/lib/arm, /system/fake-libs, /data/app/com.mycompany.myapp-2/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
                                                                                     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                     at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                                     at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                                     at com.externalibrary.client.externalibrarySettingsManager.getShouldAnnotateBlobs(externalibrarySettingsManager.java:80) 
                                                                                     at com.externalibrary.client.externalibrarySettingsManager.getMotionDetectorConfigurationFromPreference(externalibrarySettingsManager.java:39) 
                                                                                     at com.externalibrary.client.externalibrarySettingsManager.getConfigurationFromPreference(externalibrarySettingsManager.java:28) 
                                                                                     at com.externalibrary.client.externalibraryClient.<init>(externalibraryClient.java:24) 
                                                                                     at com.mycompany.myapp.MainActivity.onCreate(MainActivity.java:191) 
                                                                                     at android.app.Activity.performCreate(Activity.java:6664) 
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
    

    我可以使用cookies,但这对我来说已经好几个小时了。

    有什么想法吗?

1 个答案:

答案 0 :(得分:0)

exit()修复问题后添加header('Location: one_page.php')

Dagon说,unset($_SESSION['foo'])在重定向之前正在运行。

<强>固定

<?php
    session_start();
    if (isset($_POST['submit'])) {
        $_SESSION['foo'] = "foo";   
        header('Location: one_page.php');
        exit();
    }

    if (isset($_SESSION['foo'])) {
        unset($_SESSION['foo']);
        echo "We entered the IF";    //require('something_else.php')
    } else {
        echo "We entered the ELSE";  //require('form.php')
    }
?>
<!-- The form which is required in my case -->
<html>
    <body>
        <form method="post">
            <button name="submit">Button</button>
        </form>
    </body>
</html>

谢谢Dagon!