www.webdeveloper.com
Results 1 to 3 of 3

Thread: Check if session variable is empty

  1. #1
    Join Date
    Aug 2008
    Posts
    9

    Check if session variable is empty

    I was wondering if it is possible to check if a session variable is empty and if it is, to set it to something. For example, I have a checkout form where the buyer has the option to set a separate shipping address from their billing address. I want the shipping address to be set as the billing address if they are the same. Here's what I've tried, but it doesn't seem to be working:

    PHP Code:
    if ($_SESSION['shipping_address'] = '')
      {
        
    $_SESSION['shipping_address'] = $_REQUEST['billing_address'];
      } 
    Is there something completely wrong with what I'm doing? Any help is always appreciated! I'm still learning, so I'm sorry if this is an obvious issue!

  2. #2
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    You should put the code before any other code, above <!DOCTYPE

    PHP Code:
    ob_start();
    session_start();
    if (
    $_SESSION['shipping_address'] == '')
      {
        
    $_SESSION['shipping_address'] = $_REQUEST['billing_address'];
      } 
    ob_end_flush(); 
    I don't like using $_REQUEST because it can get data from multiple sources, I would specify $_POST if that is how the form is submitted.

    Also the if statement needs == not =.

  3. #3
    Join Date
    Aug 2008
    Posts
    9
    Quote Originally Posted by Dasher View Post
    You should put the code before any other code, above <!DOCTYPE

    PHP Code:
    ob_start();
    session_start();
    if (
    $_SESSION['shipping_address'] == '')
      {
        
    $_SESSION['shipping_address'] = $_REQUEST['billing_address'];
      } 
    ob_end_flush(); 
    I don't like using $_REQUEST because it can get data from multiple sources, I would specify $_POST if that is how the form is submitted.

    Also the if statement needs == not =.
    Excellent...thank you so much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles