#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0

    Help! Undefined variable (Session)


    Ok, so I have this problem. My session is not being recognized when I get redirected to another page.

    There are 3 steps on my website, each step has their own webpage.

    Page 1:
    PHP Code:
    <?php 
    session_start
    ();
     
    if ((!isset(
    $_SESSION['step'])) || ($_SESSION['step'] < 1)) {
          
    header("location:x.php");
        exit(); 

        
    $_SESSION['step'] = 2;

    define('STEP'3);

    $step '';
    $step $_SESSION['step'];

    if (isset(
    $_SESSION['plan'])) {
        unset(
    $_SESSION["plan"]);
        }

    if (
    $_SERVER['REQUEST_METHOD'] === 'POST') {
      
    // user posted data
      
      // data was posted from previous step. OK. update step. do not put any other code here.
      
    if ($_SESSION['step'] === (STEP 1)) {
        
    $_SESSION['step'] = STEP;
      
      
    // user can not yet access this page. ERROR. redirect user or something.
      
    } else if ($_SESSION['step'] < STEP) {
        
    // code here
          
    header("location:x.php");
        exit();
      }
    ?>
    Page 2:
    PHP Code:
    <?php 
    session_start
    ();

    if ((!isset(
    $_SESSION['step'])) || ($_SESSION['step'] < 2)) {
          
    header("location:x.php");
        exit(); 

        
    $_SESSION['step'] = 3;

    define('STEP'3);

    $step '';
    $step $_SESSION['step'];

        
    if (isset (
    $_POST['submit'])) {
        
    $_SESSION['fullname'] = $_POST['fullname'];
        
    $_SESSION['email'] = $_POST['email'];
        
    $_SESSION['phone'] = $_POST['phone'];
        unset(
    $_SESSION['fullname']);
        unset(
    $_SESSION['email']);
        unset(
    $_SESSION['phone']);
    }

    if (!isset(
    $_SESSION['plan'])) {
        
    $_SESSION['plan'] = $_GET['plan']; 
        } else {
        
    $_SESSION['plan'];
        }

    if (
    $_SERVER['REQUEST_METHOD'] === 'POST') {
      
    // user posted data
      
      // data was posted from previous step. OK. update step. do not put any other code here.
      
    if ($_SESSION['step'] === (STEP 1)) {
        
    $_SESSION['step'] = STEP;
      
      
    // user can not yet access this page. ERROR. redirect user or something.
      
    } else if ($_SESSION['step'] < STEP) {
        
    // code here
          
    header("location:x.php");
        exit();
      }
    ?>
    Page 3:
    PHP Code:
    <?php 
    error_reporting
    (E_ALL);
    session_start();

    //include "scripts/mysql.php"; 

    if ((!isset($_SESSION['step'])) || ($_SESSION['step'] < 3)) {
          
    header("location:x.php");
        exit(); 
    }

    $_SESSION['step'] = 4;

    define('STEP'3);

    $step '';
    $step $_SESSION['step'];

    $plan '';
    $SESSION_['plan'];
    $_SESSION['plan'] = $plan;

    if ((!isset(
    $_SESSION['fullname'])) || ($_SESSION['email']) || ($_SESSION['phone'])) {

        } else {
        
    $_SESSION['fullname'] = $_POST['fullname'];
        
    $_SESSION['email'] = $_POST['email'];
        
    $_SESSION['phone'] = $_POST['phone'];
        }
        

    $pname='';
    $pcost='';
    $psong='';
    $total='';

            if (
    $plan == 'plan1') {
                
    $pname "Single";
                
    $pcost "89";
                
    $psong "1";
            } else if (
    $plan == 'plan2') {
                
    $pname "Demo";
                
    $pcost "240";
                
    $psong "3";
            } else if (
    $plan == 'plan3') {
                
    $pname "EP";
                
    $pcost "415";
                
    $psong "4-6";
            } else if (
    $plan == 'plan4') {
                
    $pname "Full";
                
    $pcost "575";
                
    $psong "10-15";
            } else {
                
    $pname "ERROR";
                
    $pcost "ERROR";
                
    $psong "ERROR";
            }




    if (
    $_SERVER['REQUEST_METHOD'] === 'POST') {
      
    // user posted data
      
      // data was posted from previous step. OK. update step. do not put any other code here.
      
    if ($_SESSION['step'] === (STEP 1)) {
        
    $_SESSION['step'] = STEP;
      
      
    // user can not yet access this page. ERROR. redirect user or something.
      
    } else if ($_SESSION['step'] < STEP) {
        
    // code here
          
    header("location:x.php");
        exit();
      }
    ?>
    I should mention that the way the first page works and the way it gathers the variable for "$_SESSION['plan']" is determined from 4 different buttons (or options) the user can choose from. Here's the html for that:
    Code:
                    <form action="s2.php" method="get">
                                        <div style="background-image:url(images/pay1.png); margin-right: 28px;" id="formWrap2">
    										<div id="silv_btn">
    										<h1>&nbsp;</h1>
                                            <div class="info">&nbsp;</div>
                                            <button name="plan" type="submit" value="plan1"></button>
                                            </div> <!-- end #silv_btn -->
                                        </div> <!-- end #formWrap -->
                                        
                                        <div style="background-image:url(images/pay2.png); margin-right: 28px;" id="formWrap2">
    										<div id="silv_btn">
                                        	<h1>&nbsp;</h1>
                                            <div class="info">&nbsp;</div>
    										<button name="plan" type="submit" value="plan2"></button>
                                            </div> <!-- end #silv_btn -->
                                        </div> <!-- end #formWrap -->
                                        
                                        <div style="background-image:url(images/pay3.png); margin-right: 28px;" id="formWrap2">
                                        	<div id="gold_btn">
    										<h1>&nbsp;</h1>
                                            <div class="info">&nbsp;</div>
    										<button name="plan" type="submit" value="plan3"></button>
                                            </div> <!-- end #gold_btn -->
                                        </div> <!-- end #formWrap -->
                                        
                                        <div style="background-image:url(images/pay4.png);" id="formWrap2">
    										<div id="silv_btn">
                                        	<h1>&nbsp;</h1>
                                            <div class="info">&nbsp;</div>
    										<button name="plan" type="submit" value="plan4"></button>
                                            </div> <!-- end #silv_btn -->
                                        </div> <!-- end #formWrap -->
                    </form>
    When I choose a button from PAGE 1 (say I chose "plan2") and it redirects me to PAGE 2, I am able to pull the session data from PAGE 1 and print them onto PAGE 2. However, when I go from PAGE 2 to PAGE 3, it does not recognize the variable any longer. If anyone can help me I would me greatly appreciated!!!!


    *NOTE*
    If you would like a link to my website to see for yourself, you are more than welcome to ask for it.
  2. #2
  3. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    480
    Rep Power
    112

    Thumbs up


    You code is interesting

    Very first I am not getting what you are doing this..

    First you are making session value and later you are destroying it?
    This looks funny !!

    PHP Code:

    if (isset ($_POST['submit'])) { 
        
    $_SESSION['fullname'] = $_POST['fullname']; 
        
    $_SESSION['email'] = $_POST['email']; 
        
    $_SESSION['phone'] = $_POST['phone']; 
        unset(
    $_SESSION['fullname']); 
        unset(
    $_SESSION['email']); 
        unset(
    $_SESSION['phone']); 

    As you say you are not getting value on 3rd page by session.. It's fact thing.. See your code you are trying to fetch with "POST" method you are not using "Session" in that..

    PHP Code:

    if ((!isset($_SESSION['fullname'])) || ($_SESSION['email']) || ($_SESSION['phone'])) { 

        } else { 
        
    $_SESSION['fullname'] = $_POST['fullname']; 
        
    $_SESSION['email'] = $_POST['email']; 
        
    $_SESSION['phone'] = $_POST['phone']; 
        } 
    You haven't applied proper logic. It is very simple thing.. but you have applied very complex with dirty code !
    I will try my best !!!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by web_developer
    You code is interesting

    Very first I am not getting what you are doing this..

    First you are making session value and later you are destroying it?
    This looks funny !!

    PHP Code:

    if (isset ($_POST['submit'])) { 
        
    $_SESSION['fullname'] = $_POST['fullname']; 
        
    $_SESSION['email'] = $_POST['email']; 
        
    $_SESSION['phone'] = $_POST['phone']; 
        unset(
    $_SESSION['fullname']); 
        unset(
    $_SESSION['email']); 
        unset(
    $_SESSION['phone']); 

    As you say you are not getting value on 3rd page by session.. It's fact thing.. See your code you are trying to fetch with "POST" method you are not using "Session" in that..

    PHP Code:

    if ((!isset($_SESSION['fullname'])) || ($_SESSION['email']) || ($_SESSION['phone'])) { 

        } else { 
        
    $_SESSION['fullname'] = $_POST['fullname']; 
        
    $_SESSION['email'] = $_POST['email']; 
        
    $_SESSION['phone'] = $_POST['phone']; 
        } 
    You haven't applied proper logic. It is very simple thing.. but you have applied very complex with dirty code !
    Hey thanks for your reply. I did get the feeling I was coding things a little awkwardly lol. I just tried my best to apply logic and translate that into PHP's language.

    For your first quote, I don't know how to explain it but that was something I had to do to allow users to go back a page and change the value of their NAME, EMAIL, and/or PHONE. What ended up happening was that when they would change it, it would append their changes onto what the session already had. So I had to do interchange between SESSION and POST considerably.

    As for your second quote, those variables are actually transferring fine. It is the $_SESSION['plan'] that is not being recognized on the third page, but is being recognized on the second.

    Thanks for your response it is very much appreciated. I'm still trying to get the hang of it here :P
  6. #4
  7. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    480
    Rep Power
    112

    Thumbs up


    I am not getting why you have written this code.. But there is no sense of it. It will always destroy the session

    PHP Code:
    if (isset ($_POST['submit'])) {  
        
    $_SESSION['fullname'] = $_POST['fullname'];  
        
    $_SESSION['email'] = $_POST['email'];  
        
    $_SESSION['phone'] = $_POST['phone'];  
        unset(
    $_SESSION['fullname']);  
        unset(
    $_SESSION['email']);  
        unset(
    $_SESSION['phone']);  

    You aren't getting $_SESSION['plan'] it is also fact..

    See all below 3 of your steps for $_SESSION['plan'] It's really looks mess up

    STEP1
    By default no session will be there.. But if it found then it will unset..

    PHP Code:
    if (isset($_SESSION['plan'])) { 
        unset(
    $_SESSION["plan"]); 
        } 
    STEP2

    If Session not found then fetch "Plan" with get method.
    PHP Code:
    if (!isset($_SESSION['plan'])) { 
        
    $_SESSION['plan'] = $_GET['plan'];  
        } else { 
        
    $_SESSION['plan']; 
        } 
    STEP3
    Here it will make totally blank value for "plan"
    PHP Code:
    $plan '';   // <---- This is blank //
    $SESSION_['plan'];  
    $_SESSION['plan'] = $plan;  // <--- You are again replacing blank value in Session ??? //

    /**
       Later on you have check below conditions 
       So definitely it will always goes to the "else" condition.. Because you have already treat $plan as a blank variable. 
    **/
    if ($plan == 'plan1') { 
                
    $pname "Single"
                
    $pcost "89"
                
    $psong "1"
            } else if (
    $plan == 'plan2') { 
                
    $pname "Demo"
                
    $pcost "240"
                
    $psong "3"
            } else if (
    $plan == 'plan3') { 
                
    $pname "EP"
                
    $pcost "415"
                
    $psong "4-6"
            } else if (
    $plan == 'plan4') { 
                
    $pname "Full"
                
    $pcost "575"
                
    $psong "10-15"
            } else { 
                
    $pname "ERROR"
                
    $pcost "ERROR"
                
    $psong "ERROR"
            } 
    How value will set in $plan? Even you haven't set value in $plan by get/post/session method. Now you say how it can work?

    I will try my best !!!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by web_developer
    I am not getting why you have written this code.. But there is no sense of it. It will always destroy the session

    PHP Code:
    if (isset ($_POST['submit'])) {  
        
    $_SESSION['fullname'] = $_POST['fullname'];  
        
    $_SESSION['email'] = $_POST['email'];  
        
    $_SESSION['phone'] = $_POST['phone'];  
        unset(
    $_SESSION['fullname']);  
        unset(
    $_SESSION['email']);  
        unset(
    $_SESSION['phone']);  

    You aren't getting $_SESSION['plan'] it is also fact..

    See all below 3 of your steps for $_SESSION['plan'] It's really looks mess up

    STEP1
    By default no session will be there.. But if it found then it will unset..

    PHP Code:
    if (isset($_SESSION['plan'])) { 
        unset(
    $_SESSION["plan"]); 
        } 
    STEP2

    If Session not found then fetch "Plan" with get method.
    PHP Code:
    if (!isset($_SESSION['plan'])) { 
        
    $_SESSION['plan'] = $_GET['plan'];  
        } else { 
        
    $_SESSION['plan']; 
        } 
    STEP3
    Here it will make totally blank value for "plan"
    PHP Code:
    $plan '';   // <---- This is blank //
    $SESSION_['plan'];  
    $_SESSION['plan'] = $plan;  // <--- You are again replacing blank value in Session ??? //

    /**
       Later on you have check below conditions 
       So definitely it will always goes to the "else" condition.. Because you have already treat $plan as a blank variable. 
    **/
    if ($plan == 'plan1') { 
                
    $pname "Single"
                
    $pcost "89"
                
    $psong "1"
            } else if (
    $plan == 'plan2') { 
                
    $pname "Demo"
                
    $pcost "240"
                
    $psong "3"
            } else if (
    $plan == 'plan3') { 
                
    $pname "EP"
                
    $pcost "415"
                
    $psong "4-6"
            } else if (
    $plan == 'plan4') { 
                
    $pname "Full"
                
    $pcost "575"
                
    $psong "10-15"
            } else { 
                
    $pname "ERROR"
                
    $pcost "ERROR"
                
    $psong "ERROR"
            } 
    How value will set in $plan? Even you haven't set value in $plan by get/post/session method. Now you say how it can work?

    Thank you so much web_developer!! The fix was to replace $plan with $_SESSION['plan'] and remove $plan all together. I thought by writing $plan = ''; it would be like me stating that there is a variable named $plan like C+

IMN logo majestic logo threadwatch logo seochat tools logo