Script gets executed twice unless filename is in the URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oju
    New Member
    • May 2007
    • 6

    Script gets executed twice unless filename is in the URL

    Sometimes my scripts calls the server twice even if is a simple stright-forward one.

    I usually runs a trace in the background when I'm testing new scripts. And quite often I can see, that the server has been called two times.

    Not very smart if you're maintaining a counter across calls.



    First it was all voodoo, but I think I have found a pattern:
    If I write http://domain.com or plain www.domain.com then the script allways runs twice

    But if I add the name of the script as in http://domain.com/index.php the number of runs may be one or two.

    Can somone explain what's going on?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Changed thread title to better match contents.

    Originally posted by oju
    Can somone explain what's going on?
    Not likely, unless we had some relevant code snippets to work with.

    Comment

    • oju
      New Member
      • May 2007
      • 6

      #3
      Pew... that's a lot of code... But here it goes
      [CODE=php] <?php
      class c_tabsession {
      /* Change the values of following conatants if you like */
      const QRYNAME = "tbsi"; // "Tabbed browser session info"
      const TMPPATH = "/tmp/"; // Location to workfile
      const GCPERIOD = 3600; // Number of seconds until a untouched workfile will be deleted
      /*
      You will possible need some more constants
      for your i encrypt/decrypt algorithm
      */
      private $translink; // This is the encrypted filename and some other important data

      private $important1; // integer
      private $important2; // 32 bit hex
      private $fileId; // reference top /tmp/workfile

      public $qryin = array(); // Holds data passed by $_GET and $_POST
      public $session = array(); // Holds session data

      public function __construct()
      {
      if ( self::transInfo ( $this->translink ) )
      self::transDecr ypt( $this->translink );
      else
      { /* Make an new identifyer and lock it by redirect to self */
      $this->translink = self::transEncr ypt( $this->userId, $this->mdPass, $this->fileId );
      header( "location: " . self::href($_EN V['SCRIPT_URI'], http_build_quer y( $this->qryin )));
      exit;
      }
      /* Invariant: All variables are initiated */
      self::getSessio n();
      }

      public function __destruct()
      { /* Very important! Ensures that session-data are preserved */
      self::GarbageCo llect();
      self::putSessio n();
      }

      public function href( $uri, $QueryString = "" )
      { /* Creates URL. You are advised to use 'http_build_que ry()' to create the $QueryString */
      $uri .= "?" . http_build_quer y( array( self::QRYNAME => $this->translink) );
      if ( ! empty( $QueryString ) ) $uri .= "&" . $QueryString;
      return $uri;
      }

      public function hidden( )
      { /* creates a hidden field to be used in forms */
      return '<input name="'
      . self::QRYNAME
      . '" type="hidden" value="'
      . $this->translink
      . '">'. "\n";
      }

      public function dumpSess( $a_sess, $key = "SESSION_RO OT", $indent = "" )
      { /* Recursive dump of SESSION structure */
      if (is_array($a_se ss) )
      {
      echo "-ARRAY-<br>\n";
      foreach($a_sess AS $key => $value) self::dumpSess( $value, $key, $indent . str_repeat("&nb sp;",3) );
      return; // void
      }
      if (is_bool($a_ses s)) echo (($a_sess)?"TRU E":"FALSE"), " #BOOLEAN#<br>\n ";
      else echo $a_sess, " #", strtoupper( gettype( $a_sess )), "#<br>\n";
      return; // void
      }
      private function transInfo(&$lin k)
      {
      $link = "";
      /* Emulate $_REQUEST without $_COOKIE */
      $this->qryin = array_merge($_G ET, $_POST);
      if ( ! isset( $this->qryin[self::QRYNAME] ) )
      {
      /* First time - Generate the basic "session-id" */
      $this->important1 = self::SOME_DEFA ULT;
      $this->important2 = md5( self::ANOTHER_D EFAULT );
      $this->fileId = md5( uniqid( rand(), true ));
      return false;
      }
      $link = $this->qryin[self::QRYNAME];
      unset( $this->qryin[self::QRYNAME] );
      return true;
      }

      private function transEncrypt( $important1, $important2, $fileId )
      { // CHANGE function transDecrypt simultanious with this
      /*
      Make some encryption algorithm to mess up the parameters
      (I'd rather not disclose my own)
      */
      return $link;
      }

      private function transDecrypt( $link )
      { // DEPENTING on function transEncrypt
      /* Bring string back in correct order
      store result in object variables
      */
      return; // void
      }

      private function getSession()
      { // Read session-file into object-storage
      $path = self::SessionPa th();
      if (file_exists( $path ) )
      {
      $file = file_get_conten ts($path);
      $this->session = unserialize($fi le);
      return; // void
      }
      $this->session = array();
      return; //void
      }

      private function putSession()
      { // Save session-data on file. Invariant: $this->session in an array
      $path = self::SessionPa th();
      $file = serialize( $this->session );
      file_put_conten ts( $path, $file );
      return; // void
      }
      private function SessionPath()
      {
      $path = $_ENV['DOCUMENT_ROOT'] . self::TMPPATH;
      $file = base_convert( $this->fileId, 16, 35 ); // Compress filename
      // don't worry it's still unique
      $path .= strtoupper(self ::QRYNAME) . "_" . $file . ".txt";
      return $path;
      }

      private function GarbageCollect( )
      { // Removes sessionfiles older than GCPERIOD seconds
      $path = $_ENV['DOCUMENT_ROOT'] . self::TMPPATH;
      $path .= strtoupper(self ::QRYNAME) . "_*.txt";
      $files = glob($path);
      foreach($files as $file){
      if(is_file($fil e))
      {
      if ((filectime($fi le) + self::GCPERIOD) < time()) unlink($file);
      }
      }
      return; // void
      }
      } // End of class c_tabsession
      ?>
      <?php
      /*
      This class includes all elements that must be available to every page
      */
      class c_masterpage extends c_tabsession {


      public function __construct()
      {
      $develop = new c_construction( true ); // This line will set all subdomains to test-mode
      // move it to separate subdomains, when production starts
      parent::__const ruct(); self::test_inpu t();
      }

      public function __destruct() { self::test_outp ut(); parent::__destr uct(); } // IMPORTANT !!!

      /*************** *************** *************** *************** *************** **************
      * Test functions *
      *************** *************** *************** *************** *************** **************/

      private function test_input()
      {
      // return;
      $log = new c_syslog( $_ENV['DOCUMENT_ROOT'] . "/_ress/log/trace.txt", "Input" );
      $log->log( print_r( $_GET, true ), "GET" );
      $log->log( print_r( $_POST, true ), "POST" );
      $log->log( print_r( $_COOKIE, true ), "COOKIE" );
      $log->log( print_r( $this->session, true ), "SESSION" );
      $log->log( print_r( $_ENV, true ), "ENV" );
      }

      private function test_output()
      {
      // return;
      $log = new c_syslog( $_ENV['DOCUMENT_ROOT'] . "/_ress/log/trace.txt", "Output" );
      $log->log( print_r( $this->session, true ), "SESSION" );
      }

      private function test_trace( $arg )
      {
      static $counter = 0; $counter++;
      $log = new c_syslog( $_ENV['DOCUMENT_ROOT'] . "/_ress/log/trace.txt", "Trace" );
      $log->log( $arg , $counter );
      }

      } // End of class c_masterpage
      ?>
      <?php
      $page = new masterpage();
      $page->session['counter']++;
      ?>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <title>Dummy</title>
      </head>
      <body>
      </body>
      </html>
      [/CODE]
      Last edited by pbmods; May 27 '07, 10:47 PM. Reason: Changed code language. Thanks for using CODE tags!

      Comment

      • oju
        New Member
        • May 2007
        • 6

        #4
        Cannot send my trace - gets wierd error-message :(

        Comment

        • oju
          New Member
          • May 2007
          • 6

          #5
          Originally posted by oju
          Cannot send my trace - gets wierd error-message :(
          You can read it on www.junne.dk/drill/double.txt

          Comment

          • oju
            New Member
            • May 2007
            • 6

            #6
            I've found the perpetrator, but not the reason.

            The last part of my HTML-code goes like this:
            [HTML] <div id="siteInfo"> <img src="" width="44" height="22"> <a href="#">Om dette web-sted </a> | <a href="#">Teknis ke Krav </a> | <a href="#">Din Sikkerhed </a> | <a href="#">Webmas ter</a> | &copy;2007 Antvorskov Kirke </div>
            [/HTML]
            And it is id="siteInfo", that does the damage. When I remove this, the loop stops. "siteInfo" has a reference to my css stylesheet, saying:
            Code:
             #siteInfo{
            clear: both;
            border-top: 1px solid #cccccc;
            font-size: small;
            color: #cccccc;
            padding: 10px 10px 10px 10px;
            margin-top: 0px;
            }
            But it looks quite stright-forward. So I'm clueless :((

            Anyway - now I have removed the reference so I can get on with my work.

            Comment

            • oju
              New Member
              • May 2007
              • 6

              #7
              It came back - this annoying double server-call. And this time I could count out errors in CSS. But then I noticed something. I'm working with a template of the fill-in-the-blanks kind, and I had'nt filled in the images yet, so I had a line like this:

              [HTML] <img src="" alt="" width="119" height="180"> [/HTML]

              - and BINGO! I got two calls to the server. Now examine the css-code I posted last time: It had an empty image-source too.

              I still don't know why, but just take care out there :-)

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Oju.

                Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

                Comment

                Working...