By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

Script gets executed twice unless filename is in the URL

P: 6
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?
May 26 '07 #1
Share this Question
Share on Google+
7 Replies


pbmods
Expert 5K+
P: 5,821
Changed thread title to better match contents.

Can somone explain what's going on?
Not likely, unless we had some relevant code snippets to work with.
May 26 '07 #2

P: 6
oju
Pew... that's a lot of code... But here it goes
Expand|Select|Wrap|Line Numbers
  1.  <?php 
  2. class c_tabsession {
  3. /* Change the values of following conatants if you like */
  4. const QRYNAME = "tbsi"; // "Tabbed browser session info"
  5. const TMPPATH = "/tmp/"; // Location to workfile 
  6. const GCPERIOD = 3600; // Number of seconds until a untouched workfile will be deleted
  7. /* 
  8. You will possible need some more constants 
  9. for your i encrypt/decrypt algorithm 
  10. */
  11. private $translink; // This is the encrypted filename and some other important data
  12.  
  13. private $important1; // integer
  14. private $important2; // 32 bit hex
  15. private $fileId; // reference top /tmp/workfile
  16.  
  17. public $qryin = array(); // Holds data passed by $_GET and $_POST
  18. public $session = array(); // Holds session data
  19.  
  20. public function __construct()
  21. {
  22. if ( self::transInfo( $this->translink ) )
  23.     self::transDecrypt( $this->translink );
  24. else
  25.     { /* Make an new identifyer and lock it by redirect to self */
  26.      $this->translink = self::transEncrypt( $this->userId, $this->mdPass, $this->fileId );
  27.      header( "location: " . self::href($_ENV['SCRIPT_URI'], http_build_query( $this->qryin )));
  28.      exit; 
  29.     }
  30. /* Invariant: All variables are initiated */
  31. self::getSession(); 
  32. }
  33.  
  34. public function __destruct()
  35. { /* Very important! Ensures that session-data are preserved */
  36. self::GarbageCollect();
  37. self::putSession();
  38. }
  39.  
  40. public function href( $uri, $QueryString = "" )
  41. { /* Creates URL. You are advised to use 'http_build_query()' to create the $QueryString */
  42.      $uri .= "?" . http_build_query( array( self::QRYNAME => $this->translink) );
  43. if ( ! empty( $QueryString ) ) $uri .= "&" . $QueryString;
  44. return $uri;
  45.  
  46. public function hidden( )
  47. { /* creates a hidden field to be used in forms */
  48. return '<input name="'
  49.         . self::QRYNAME
  50.      . '" type="hidden" value="'
  51.      . $this->translink
  52.      . '">'. "\n";
  53. }
  54.  
  55. public function dumpSess( $a_sess, $key = "SESSION_ROOT", $indent = "" )
  56. { /* Recursive dump of SESSION structure */
  57. if (is_array($a_sess) )
  58. {
  59.     echo "-ARRAY-<br>\n";
  60.     foreach($a_sess AS $key => $value) self::dumpSess( $value, $key, $indent . str_repeat("&nbsp;",3) );
  61.     return; // void
  62. }
  63. if (is_bool($a_sess)) echo (($a_sess)?"TRUE":"FALSE"), " #BOOLEAN#<br>\n";
  64. else echo $a_sess, " #", strtoupper( gettype( $a_sess )), "#<br>\n";
  65. return; // void
  66. private function transInfo(&$link)
  67. {
  68. $link = "";
  69. /* Emulate $_REQUEST without $_COOKIE */
  70. $this->qryin = array_merge($_GET, $_POST);
  71. if ( ! isset( $this->qryin[self::QRYNAME] ) )
  72. {
  73. /* First time - Generate the basic "session-id" */
  74.     $this->important1 = self::SOME_DEFAULT;
  75.     $this->important2 = md5( self::ANOTHER_DEFAULT );
  76.     $this->fileId = md5( uniqid( rand(), true ));
  77.     return false;
  78. }
  79. $link = $this->qryin[self::QRYNAME];
  80. unset( $this->qryin[self::QRYNAME] );
  81. return true;
  82. }
  83.  
  84. private function transEncrypt( $important1, $important2, $fileId )
  85. { // CHANGE function transDecrypt simultanious with this
  86. /* 
  87.     Make some encryption algorithm to mess up the parameters
  88.     (I'd rather not disclose my own)
  89. */
  90.      return $link;
  91. }
  92.  
  93. private function transDecrypt( $link )
  94. { // DEPENTING on function transEncrypt
  95. /* Bring string back in correct order
  96.     store result in object variables 
  97.     */
  98. return; // void
  99. }
  100.  
  101. private function getSession()
  102. { // Read session-file into object-storage
  103. $path = self::SessionPath();
  104. if (file_exists( $path ) ) 
  105. {
  106.     $file = file_get_contents($path);
  107.     $this->session = unserialize($file);
  108.     return; // void
  109. }
  110. $this->session = array();
  111. return; //void
  112. }
  113.  
  114. private function putSession()
  115. { // Save session-data on file. Invariant: $this->session in an array
  116. $path = self::SessionPath();
  117. $file = serialize( $this->session );
  118. file_put_contents( $path, $file );
  119. return; // void 
  120. }
  121. private function SessionPath()
  122. {
  123. $path = $_ENV['DOCUMENT_ROOT'] . self::TMPPATH;
  124. $file = base_convert( $this->fileId, 16, 35 ); // Compress filename
  125.              // don't worry it's still unique
  126. $path .= strtoupper(self::QRYNAME) . "_" . $file . ".txt";
  127. return $path;
  128. }
  129.  
  130. private function GarbageCollect()
  131. { // Removes sessionfiles older than GCPERIOD seconds
  132. $path = $_ENV['DOCUMENT_ROOT'] . self::TMPPATH;
  133. $path .= strtoupper(self::QRYNAME) . "_*.txt";
  134. $files = glob($path);
  135. foreach($files as $file){
  136.     if(is_file($file)) 
  137.     {
  138.      if ((filectime($file) + self::GCPERIOD) < time()) unlink($file);
  139.     }
  140. }
  141. return; // void
  142. }
  143. } // End of class c_tabsession
  144. ?>
  145. <?php 
  146. /*
  147. This class includes all elements that must be available to every page 
  148. */
  149. class c_masterpage extends c_tabsession {
  150.  
  151.  
  152. public function __construct()
  153. {
  154. $develop = new c_construction( true ); // This line will set all subdomains to test-mode
  155.              // move it to separate subdomains, when production starts
  156. parent::__construct(); self::test_input(); 
  157. }
  158.  
  159. public function __destruct() { self::test_output(); parent::__destruct(); } // IMPORTANT !!!
  160.  
  161. /***************************************************************************************** 
  162. *                             Test functions                                         * 
  163. *****************************************************************************************/
  164.  
  165. private function test_input()
  166. {
  167. // return;
  168. $log = new c_syslog( $_ENV['DOCUMENT_ROOT'] . "/_ress/log/trace.txt", "Input" );
  169. $log->log( print_r( $_GET, true ), "GET" );
  170. $log->log( print_r( $_POST, true ), "POST" );
  171. $log->log( print_r( $_COOKIE, true ), "COOKIE" );
  172. $log->log( print_r( $this->session, true ), "SESSION" );
  173. $log->log( print_r( $_ENV, true ), "ENV" );
  174. }
  175.  
  176. private function test_output()
  177. {
  178. // return;
  179. $log = new c_syslog( $_ENV['DOCUMENT_ROOT'] . "/_ress/log/trace.txt", "Output" );
  180. $log->log( print_r( $this->session, true ), "SESSION" );
  181. }
  182.  
  183. private function test_trace( $arg )
  184. {
  185. static $counter = 0; $counter++;
  186. $log = new c_syslog( $_ENV['DOCUMENT_ROOT'] . "/_ress/log/trace.txt", "Trace" );
  187. $log->log( $arg , $counter );
  188. }
  189.  
  190. } // End of class c_masterpage
  191. ?>
  192. <?php
  193. $page = new masterpage();
  194. $page->session['counter']++;
  195. ?>
  196. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  197. "http://www.w3.org/TR/html4/loose.dtd">
  198. <html>
  199. <head>
  200. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  201. <title>Dummy</title>
  202. </head>
  203. <body>
  204. </body>
  205. </html>
  206.  
May 27 '07 #3

P: 6
oju
Cannot send my trace - gets wierd error-message :(
May 27 '07 #4

P: 6
oju
Cannot send my trace - gets wierd error-message :(
You can read it on www.junne.dk/drill/double.txt
May 27 '07 #5

P: 6
oju
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="#">Tekniske Krav </a> | <a href="#">Din Sikkerhed </a> | <a href="#">Webmaster</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:
Expand|Select|Wrap|Line Numbers
  1.  #siteInfo{
  2. clear: both;
  3. border-top: 1px solid #cccccc;
  4. font-size: small;
  5. color: #cccccc;
  6. padding: 10px 10px 10px 10px;
  7. margin-top: 0px;
  8. }
  9.  
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.
May 29 '07 #6

P: 6
oju
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 :-)
Jul 20 '07 #7

pbmods
Expert 5K+
P: 5,821
Heya, Oju.

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

Post your reply

Sign in to post your reply or Sign up for a free account.