The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> PHP is operating differently on live site than local server
Technetium
post Oct 4 2011, 10:21 AM
Post #1


Newbie
*

Group: Members
Posts: 12
Joined: 16-March 11
Member No.: 14,144



I have some PHP sessions on my photography website that I am using to create a simple order form / cart. When viewing gallery.php, it starts a session and toggles a variable $_SESSION["session_started"] to be true. When the submit button on that page is clicked to order a print of a photo, it adds the print to the cart, and goes to order.php. If the sessions_started variable is not set to true (meaning it somehow got to the order page without going to the gallery first and no images are selected), it sets a variable bad_link to true and then in the <head> tag, if bad_value is true it uses a meta tag to change the URL to the home page.

All of this works fine in the offline server I have set up on my laptop for development, and it properly populates the list of . However, when the site is put online (I'm using freeyellow to host), any attempt to add a print to the cart goes to the home page. I did some testing to try and narrow down the problem:

- I added a line at the bottom of the gallery.php page to print some text if $_SESSION["session_started"] is true. This is because I was concerned that the online version was perhaps disabling sessions altogether. It prints the line of text in both the offline and online versions, so I can confirm the session is started.

- My suspicion is that the online site is not carrying session data from one page to the next (which kind of defeats the purpose of having sessions).

Here is the relevant code:

First part of gallery.php, before the <html> tag.
CODE

<?php
  session_start();
  if(!isset($_SESSION["session_started"]))
  {
    $_SESSION["session_started"]=true;
    $_SESSION["items_in_cart"]=0;
  }
?>


The code in the <head> tag of gallery.php responsible for transitioning to order.php after the submit button is clicked.
CODE

      $errors="";
      if(isset($_GET['submit']))
      {
        if(empty($_POST["print_select"]))
        {
          $errors.="<p style='color: #FF0000;'>Error: You must select a print size.</p>";
        }
        if(!$errors)
        {
          $_SESSION["item"][$_SESSION["items_in_cart"]]["ID"]=$flickr_ID;
          $_SESSION["item"][$_SESSION["items_in_cart"]]["size"]=$_POST["print_select"];
          $_SESSION["item"][$_SESSION["items_in_cart"]]["number"]=$_POST["number_of_prints"];
          $_SESSION["items_in_cart"]++;
          echo ("<meta http-equiv='refresh' content='0; url=order.php'>");
        }
      }


The submit button is a standard form submit button, no PHP involved in that. I should mention that everything works correctly up to this point, in that it does follow the meta tag URL change properly, and only when the submit button has been clicked.

The whole order.php file, note that this file is a work-in-progress and other parts of this file are not complete yet
CODE

<?php
  session_start();
  $bad_link=false;
  if(!isset($_SESSION["session_started"]))
  {
    $bad_link=true;
  }
?>
<html>
  <head>
    <?php
      if($bad_link)
      {
        echo ("<meta http-equiv='refresh' content='0; url=news.php'>"); //This is where the problem seems to occur, it always executes this on the live site, but not offline.
      }
    ?>
    <title>Justin Smith Photography</title>
    <link rel="stylesheet" type="text/css" href="main.css">
    <script src="jscript.js"></script>
    <?php
      if(!empty($_POST["delete"]))
      {
        for($i=0;$i<$_SESSION["items_in_cart"];$i++)
        {
          if($_POST["deleteme".$i]==$i)
          {
            $_SESSION["item"][$i]["number"]=0;
          }
        }
        $itemsleft=0;
        for($i=0;$i<$_SESSION["items_in_cart"];$i++)
        {
          $itemsleft+=$_SESSION["item"][$i]["number"];
        }
        if($itemsleft == 0)
        {
          $_SESSION["items_in_cart"]=0;
          session_destroy();
          echo ("<meta http-equiv='refresh' content='0; url=news.php'>");
        }
      }
      else if(!empty($_POST["clear"]))
      {
        for($i=0;$i<$_SESSION["items_in_cart"];$i++)
        {
          $_SESSION["item"][$i]["number"]=0;
        }
        $_SESSION["items_in_cart"]=0;
        session_destroy();
        echo ("<meta http-equiv='refresh' content='0; url=news.php'>");
      }

      $errors="";
      if(!empty($_POST["order"]))
      {
        if(empty($_POST["firstname"]))
        {
          $errors.="<p style='color: #FF0000;'>Error: You must enter your first name.</p>";
        }
        if(empty($_POST["lastname"]))
        {
          $errors.="<p style='color: #FF0000;'>Error: You must enter your last name.</p>";
        }
        if(empty($_POST["address1"]))
        {
          $errors.="<p style='color: #FF0000;'>Error: You must enter your street address.</p>";
        }
        if(empty($_POST["address3"]))
        {
          $errors.="<p style='color: #FF0000;'>Error: You must enter your city or town.</p>";
        }
        if(empty($_POST["address4"]))
        {
          $errors.="<p style='color: #FF0000;'>Error: You must enter your zip code.</p>";
        }
        if(empty($_POST["emailaddress"]))
        {
          $errors.="<p style='color: #FF0000;'>Error: You must enter an email address.</p>";
        }
        else if(!preg_match("/@/",$_POST["emailaddress"]) || preg_match("/'/",$_POST["emailaddress"]) || preg_match('/"/', $_POST["emailaddress"]))
        {
          $errors.="<p style='color: #FF0000;'>Error: Email address not valid.</p>";
        }
        $totalprints=0;
        for($i=0;$i<$_SESSION["items_in_cart"];$i++)
        {
          $totalprints+=$_SESSION["item"][$i]["number"];
        }
        if(!$totalprints)
        {
          $errors.="<p style='color: #FF0000;'>Error: No prints in queue.</p>";
        }
        if(!$errors)
        {
          $email_from="justinsmith@justinsmithphoto.com";
          $email_subject="Print Order Request";
          $email_body="Request for order!\n\n".
                      $_POST["firstname"]."\n".
                      $_POST["lastname"]."\n".
                      $_POST["address1"]."\n".
                      $_POST["address2"]."\n".
                      $_POST["address3"].", ".
                      $_POST["address4"]." ".
                      $_POST["address5"]."\n".
                      $_POST["emailaddress"]."\n\n";
          $email_orderlist="";
          for($i=0;$i<$_SESSION["items_in_cart"];$i++)
          {
            if($_SESSION["item"][$i]["number"])
            {
              $email_orderlist.="IMAGE ID: ".$_SESSION["item"][$i]["ID"]."\n".
                           "Size: ".$_SESSION["item"][$i]["size"]."\n".
                           "Number of prints: ".$_SESSION["item"][$i]["number"]."\n\n";
            }
          }
          $email_body.=$email_orderlist;
          mail($email_from,$email_subject,$email_body,"From: ".$email_from." \r\n Reply-To: ".$_POST["text_email"]." \r\n");
          $email_body2="Thanks for your request! \n\n".
                       "You ordered: \n".
                       $email_orderlist.
                       "I will respond to your request as soon as possible.";
          mail($_POST["text_email"],$email_subject,$email_body2,"From: ".$email_from." \r\n Reply-To: ".$email_from." \r\n");
          session_destroy();
          echo ("<meta http-equiv='refresh' content='0; url=ordercomplete.php'>");
        }
      }
    ?>
  </head>
  <body>
    <br>
    <table class="main" style="margin-left: auto; margin-right:auto; width:">
      <tr>
        <td>
          <table class="main">
            <tr>
              <td width=448 height=100><img src="logo.gif"></td>
              <td width=20></td>
              <td style="vertical-align: bottom;">
                <div class="mainlink"><a href="news.php"> News </a></div>
              </td>
              <td width=20></td>
              <td style="vertical-align: bottom;">
                <div class="mainlink"><a href="galleries.html"> Galleries </a></div>
              </td>
              <td width=20></td>
              <td style="vertical-align: bottom;">
                <div class="mainlink"><a href="prints.php"> Prints </a></div>
              </td>
              <td width=20></td>
              <td style="vertical-align: bottom;">
                <div class="mainlink"><a href="about.html"> About </a></div>
              </td>
              <td width=20></td>
              <td style="vertical-align: bottom;">
                <div class="mainlink"><a href="links.html"> Links </a></div>
              </td>
              <td width=20></td>
            </tr>
          </table>
        </td>
      </tr>

      <tr>
        <td height=2 bgcolor="#000000"></td>
      </tr>
      <tr>
        <td height=1 bgcolor="#00FF00"></td>
      </tr>
      <tr>
        <td height=2 bgcolor="#000000"></td>
      </tr>

      <tr>
        <td>
          <table class="main" cellpadding=5 width=800>
            <tr>
              <td>
                <p>::Order form::</p>
                <?php
                  if($errors)
                  {
                    echo ($errors);
                  }
                ?>
                <center><form name="order_form" method="POST" action="order.php">
                  <div class="datelist" style="width: 600px;">
                    <table width=100%>
                      <?php
                        $total_cost=0;
                        for($i=0;$i<$_SESSION["items_in_cart"];$i++)
                        {
                          if($_SESSION["item"][$i]["number"])
                          {
                            echo ("<tr style='border-color: #FFFFFF; border-style: solid; border-width: 1px;'>");
                            echo ("<td style='vertical-align: middle; text-align: center;'>");
                            echo ("<input type='checkbox' name='deleteme".$i."' value='".$i."'></input>");
                            echo ("</td>");
                            $imagedescfile=fopen("imagedesc.txt","r");
                            $currentline=preg_replace("/\r?\n$/","",fgets($imagedescfile));
                            while($currentline!=$_SESSION["item"][$i]["ID"])
                              {
                                $currentline=preg_replace("/\r?\n$/","",fgets($imagedescfile));
                            }
                            $currentline=preg_replace("/\r?\n$/","",fgets($imagedescfile));
                            $temp=explode(" ",$currentline);
                            $imagename=$temp[0];
                            fclose($imagedescfile);
                            echo ("<td class='thumbnail_holder'><div class='thumbnail_holder'>");
                            echo ("<div class='thumbnail_space'><img class='thumbnail' src='thumbnails/".$imagename."'></div>");
                            echo ("</div></td>");
                            echo ("<td style='vertical-align: top; text-align: left;'>");
                            if($_SESSION["item"][$i]["size"]=="4x6" || $_SESSION["item"][$i]["size"]=="4x5")
                            {
                              $cost=4;
                            }
                            else if($_SESSION["item"][$i]["size"]=="6x9")
                            {
                              $cost=14;
                            }
                            else if($_SESSION["item"][$i]["size"]=="8x12" || $_SESSION["item"][$i]["size"]=="8x10")
                            {
                              $cost=20;
                            }
                            else if($_SESSION["item"][$i]["size"]=="10x15")
                            {
                              $cost=30;
                            }
                            else if($_SESSION["item"][$i]["size"]=="12x18" || $_SESSION["item"][$i]["size"]=="12x15")
                            {
                              $cost=40;
                            }
                            echo ("<p>Print size: ".$_SESSION["item"][$i]["size"]." @ $".$cost.".00 each</p>");
                            echo ("<p>Number of prints: ".$_SESSION["item"][$i]["number"]);
                            echo ("<p>Total of this item: $".($_SESSION["item"][$i]["number"]*$cost).".00</p>");
                            $total_cost+=($_SESSION["item"][$i]["number"]*$cost);
                            echo ("</td>");
                            echo ("</tr>");
                          }
                        }
                      ?>
                      <tr>
                        <td style="background-color: #000000; color: #FFFFFF; text-align: right;" colspan=3>
                          <?php
                            echo ("<p>Total Price: $".$total_cost.".00</p>")
                          ?>
                        </td>
                      </tr>
                    </table>
                  </div>
                  <div style="width: 600px;">
                    <input type="submit" name="delete" value="Deleted Selected"><input type="submit" name="clear" value="Clear Order">
                  <div style="width: 600px;">
                    <table>
                      <tr>
                        <td style="text-align: right; color: #FFFFFF;">
                          <p>First name: <input type="text" name="firstname"></p>
                          <p>Last name: <input type="text" name="lastname"></p>
                          <p>Address: <input type="text" name="address1"></p>
                          <p>Apt/Suite #: <input type="text" name="address2"></p>
                          <p>City: <input type="text" name="address3"></p>
                          <p>State: <input type="text" name="address4"></p>
                          <p>Zip Code: <input type="text" name="address5"></p>
                          <p>Email: <input type="text" name="emailaddress"></p>
                          <input type="submit" name="order" value="Submit Order">
                        </td>
                      </tr>
                    </table>
                  </div>
                </form></center>
              </td>
            <tr>
          </table>
        </td>
      </tr>
    </table>
    <center>all content copyright <a href="mailto: justinsmith@justinsmithphoto.com">Justin Smith</a> 2011<br>
    </center>
  </body>
</html>


Anyway, as I said before, this works flawlessly on my offline server. It's just on freeyellow that it doesn't work. Do you think it's something specific with freeyellow?

This post has been edited by Technetium: Oct 4 2011, 10:37 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Oct 4 2011, 03:24 PM
Post #2


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



Look for phpinfo in php.net and see if the two versions of php are the same locally and on your site.


--------------------
Ephraim F. Moya
"Los pintos y los pendejos
Se conocen desde lejos."
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Technetium
post Oct 4 2011, 03:33 PM
Post #3


Newbie
*

Group: Members
Posts: 12
Joined: 16-March 11
Member No.: 14,144



I figured out what the problem is. I should have checked at freeyellow.com before posting here. They have a php.ini file that I have to edit to enable sessions to work properly. That part of the file tells the host where to save the temporary session variables.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 22nd December 2014 - 10:46 PM