$_SESSION variable not passing and no clue why? it sets then doesn't pass

戏子无情 提交于 2019-12-20 07:35:36

问题


I have a login script coded in php and mysqli. upon submission and successful authentication it redirects to success.php to write sessions. my success page looks like this.

<?php  
 /**
     * Set cookies here if/as needed.
     * Set session data as needed. DO NOT store user's password in
     * cookies or sessions!
     * Redirect the user if/as required.
     */

     $id = $_GET['id'];

    $_SESSION['partner_id']       = $id;
    $_SESSION['authenticated'] = TRUE;

    if (isset($_SESSION['partner_id'])) {
    echo("<script>
    <!--
    location.replace(index.php);
    -->
    </script>");

    }

    else {
        print "Session partner id not set";
    }

?>

This page effectively redirects to my index.php page. However my index php does not recognize the session variable.

    <?php
 session_start();
 // rest of code
print "ths is the session var".$_SESSION['partner_id'];
    ?>

that is at the top of the page before the doctype declaration.

on the page i also have

<?php 
        if(isset($_SESSION['partner_id'])) {
        print "This is the session id variable".$_SESSION['partner_id']; 
        }

        else {
        print "Session not set";
        }

        ?>

The top check prints blank and the bottom one prints session not set....

I am confused...


回答1:


You need session_start() on all pages using $_SESSION

  <?php  
 /**
 * Set cookies here if/as needed.
 * Set session data as needed. DO NOT store user's password in
 * cookies or sessions!
 * Redirect the user if/as required.
 */
 session_start();

 $id = $_GET['id'];

$_SESSION['partner_id']       = $id;
$_SESSION['authenticated'] = TRUE;

Also...are you sure that $_GET['id'] has a value in it?

And....is your form using POST or GET?

Ive made that silly mistake before , where form was using POST and I was retrieving variable with GET and getting frustrated. Just a possibility




回答2:


Use session_start(); inside all page before using $_SESSION variable in PHP. session_start(); function use to initialize session process.

your success.php should be.

<?php  
/**
 * Set cookies here if/as needed.
 * Set session data as needed. DO NOT store user's password in
 * cookies or sessions!
 * Redirect the user if/as required.
 */

 session_start();

 $id = $_GET['id'];

$_SESSION['partner_id']       = $id;
$_SESSION['authenticated'] = TRUE;

if (isset($_SESSION['partner_id'])) {
echo("<script>
<!--
location.replace(index.php);
-->
</script>");

}

else {
    print "Session partner id not set";
}

?>



回答3:


The initial file didn't contain session_start() as pointed out. however it works without it for some reason now.

I am thinking a cache or server delay issue also




回答4:


Adding to answers, If you don't want to write session_start() on starting of each and every page, You can simply skip it by setting the session.auto_start = 1 in your php.ini file , which starts the session automatically on request page. Refer to PHP Session Configuration Documentation.



来源:https://stackoverflow.com/questions/24482937/session-variable-not-passing-and-no-clue-why-it-sets-then-doesnt-pass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!