开发者

My $_SESSION[''] disappears

Here is my problem, I have a PHP SESSION but it disappears...

That's my code,

<?php
    session_start();
    if (isset($_SESSION['connect'])) {
        echo $_SESSION['email'];    
        echo '<meta http-equiv="refresh" content="6;URL=http://www.mywebsite.com/management.php">';
    } 
?>

$_SESSION['connect'] exists, so I pass the If check.

When I display $_SESSION['email'] it works. $_SESSION['emai开发者_开发技巧l'] = mymail@gmail.com.

But when I'm redirected on my page management.php $_SESSION['email'] = 1.

management.php

<?php
    session_start();
    echo $_SESSION['email'];
?>

Why?

Thanks.


is this your complete code? if so, you need to add session_start() to use sessions.

(see: http://php.net/manual/en/function.session-start.php )

After question edit: it looks like you have sessoin_start() on this page. But do you have it on the management.php page?


are you redirecting accross different domains? If so, you could try to set the name of your session before session_start()

To do that, you have to submit your sessionname via get-parameter in the redirect

echo '<meta http-equiv="refresh" content="6;URL=http://www.mywebsite.com/management.php?sessionname='. session_name() .'">';

and then use

session_name($_GET['sessionname']);
session_start();

in management.php


you must have session_start() on management.php


set.php

<?php

session_start();

/* prevent XSS. */
$_GET   = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);

if (isset($_GET['session'])) {
    $_SESSION['session'] = $_GET['session'];
    echo 'set succesfully';
} else {
    echo 'use ?session=<your data> to set session data';
}

get.php

<?php

session_start();

/* prevent XSS. */
$_GET   = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);

if (isset($_SESSION['session'])) {
    echo $_SESSION['session'];
} else {
    echo 'not set yet';
}

setget.php

<?php

session_start();

/* prevent XSS. */
$_GET   = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);

if (isset($_GET['session'])) {
     $_SESSION['session'] = $_GET['session'];
    header('Location: get.php');
    exit();
} else {
    echo 'use ?session=<your data> to set session data';
}

I have these php files(in a single folder) mapped to http://localhost/stackoverflow/4826773/

  1. http://localhost/stackoverflow/4826773/set.php?session=test

    output: set succesfully

  2. http://localhost/stackoverflow/4826773/get.php

    output: test

  3. http://localhost/stackoverflow/4826773/setget.php?session=hello

    output: hello

This is what you want right? If you get different results then something is broken!

P.S: I think using

<meta http-equiv="refresh" content="6;URL=http://www.mywebsite.com/management.php">

is not consider a best practice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜