简体   繁体   中英

how to use session in multiple pages

        // first page
            <?php
            session_start();
            //   $var_value =$_SESSION['orderidno'];
            //   $var_value =$_GET['orderur'];
               
            
            if(isset($_POST['login']))
            {
                $mobileNo=$_POST['mobileNo'];
                $passWord=$_POST['passWord'];
            }
            
             $curl = curl_init();
            
            curl_setopt_array($curl, array(
              CURLOPT_URL => 'here is my api url',
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'GET',
             
            ));
            
            $response_details = curl_exec($curl);
            $data_details=json_decode($response_details,TRUE);
            $data1_details=json_decode($response_details);
            curl_close($curl);
            
              $res=$data_details['ispasswordMatched'];
               
                if($res)
                {
                    
               $_SESSION['loggedin'] = "true";
               $_SESSION['loggedinnumber'] = $mobileNo;
               foreach($data_details['content'] as $result)
               {
                    $_SESSION['vendorname'] = $result['vendorname'];
               $_SESSION['vendorkey'] = $result['vendorkey'];
               $_SESSION['userkey'] = $result['key'];
               //here i have been storing my api result values in session
               }
              echo "<script> location.href='userdetails.php'; </script>";
                }
                else if($mobileNo!="")
                {
                    $empty=$data_details['message'];
                }
            ?>
            
            <!DOCTYPE html>
            <html>
                <head>
                    
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <title>PHP HTML TABLE DATA SEARCH</title>
                    <style>
                        table,tr,th,td
                        {
                            border: 1px solid black;
                        }
                        div#searcharea {
                text-align: center;
            }
                    </style>
                </head>
                <body>
            
            
                        <form id="search_form" method="post"  action="login.php" >
              <!--<h4>Search Customer / Customer Order</h4>-->
              <div>
                <input id="mobilenumbervalue" style="width: 88%; margin-top: 22px" type="text" name="mobileNo" placeholder="Enter the Mobile Number" pattern="[1-9]{1}[0-9]{9}" required>  
                   <input id="orderidvalue" style="width: 88%; margin-top: 22px" type="password" name="passWord" placeholder="Enter the password"  required>
                <br>
              
                    <p id="mobileError" style="color: red;"><?php echo $empty?></p>
                    <p id="orderError" style="color: red;"><?php echo $emptyOrder?></p>
                
                
              </div>
              <div>
                <Button style="background: #fdd110;      width: 35%;
                height: 39px;   border: #fdd110; margin-top: 15px;" id="userdataSearchButton" type="submit" name="login" >Log In</Button>
              </div>
            </form>
            
                    
                    <script  type="text/javascript">
                    
                    
            function myFunction() {
                alert("work in progress");
            }
            
            
            
            function required()
            {
            var empt = document.forms["form1"]["valueToSearch"].value;
            if (empt == "")
            {
            alert("Please input a Value");
            return false;
            }
            
            }
            </script>
                </body>
            </html>
    //end of first page
        //second page
        //userdetails.php
        <?php
        session_start();
        $var_value =$_SESSION['loggedin'];
        if($var_value!="true")
        {
            echo "the user doesn't exist";
        }
        else
        {
        echo "the user is exists";
        }
        ?>
//here will be my second page html 
    //end of second page

what i am doing

  1. In the first part I have been checking the database if the mobile number and password exists .If the mobile number exists then I have been saving them in session to use in another pages and in '$_SESSION['loggedin'] = "true"; setting session as true'.
  2. In the second page I have been checking if the user exists by checking if the session is true but the session value is empty.

Problem

  1. In second page I have been checking if its value is true but its value is empty so I can't let user to use the page.
  2. All other session value is also empty.

Description

  1. This program was working while I hosting them in godaddy .
  2. after I changed to the new server the session is not working like it was working in godaddy

Working Method

  1. If I run these in separate php and call via ajax it is working the session value is 'true' but all my codes are like in first page php, html and javascript all are in single page .
  2. I need a solution how to run them in a single page.

*. This session problem only arise if I use these codes in my new linux server hosting , But in godaddy linux hosting is working fine.

Comment me if any additional information needed.

Since you experienced a different behaviour in two different ambients, I would check the php configuration in the new server for session handling, that is 'session.cookie_httponly' (since you use javascript for redirection), 'session.use_cookies' and 'session.use_only_cookies' .

Besides, I would suggest to force-close the session before doing a redirection, to ensure the session actually writes the data before the new page loads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM