Skip navigation.

Display Controls
Large text Medium text Small text (default) Change to High Contrast Change to Default Color Scheme
Last Update: Jan 06 2009

  Sustainability Tables

Search Sustainability Resources


  As of Oct 2006

Who's been visiting SN?
  Path http://for-legacies-sake.ca/ —  home > extras > php lessons
Search SN Website     
Print Content
 

Website was originally in .lasso and I needed to convert existing pages to php.

Objective 1: Needed a method of detecting and redirecting pages with .lasso extensions to .php

  • Problem: I assumed that any webpage names ending in extensions other than php would result in a 404 error. Wrong!
    • Assume that pagename.php exists. pagename..php will generate a 404 error, but pagnam.php will cause a 500 error.
    When I tried to set up an ErrorDocument, not realizing the problem, my first attempt to test the results of my error page by using the pagnam.php format kept bringing up the Error 500 and not the Error 404 webpage. This made is even more difficult for me to resolve ErrorDocument directives.

 


Objective 2: Place ErrorDocument at website root directory rather than in htdocs directory.

  • Even if I force the .htaccess to access the ErrorDocuments from the website root directory, the actual ErrorDocument page loaded and lost all of the php info required to rebuild a corrected redirect (i.e. lasso pages to their php replacements). Therefore, the ErrorDocuments must go into htdoc to work.

 


Objective 3: Testing ErrorDocument 500 which occurs if a php page does not exist.

  • Observation 1: Even though the ErrorDocument worked for 404 (see above) a misspelled pagename with a php extension results in an error 500 and the .htaccess seems to be ignored.
  • Observation 2: A misspelled subdirectory even with a php extension in the (misspelled) pagename accessed the customized 404 ErrorDocument.
  • Observation 3: Removing the ErrorDocument directive from the .htaccess file and enabling the directive in httpd.conf around line 887 by removing the '#' and insuring the correct file name ... accessed the customized file. There doesn't seem to be anything to do with altering the AllowOverride directive.
  • Conclusion Given the inconsistencies as detailed above, using the .htaccess file for just ErrorDocuments doesn't seem worth the trouble.

 

Print Content || Go to Site Directory || Up || Dwn  

Objective 4: Saving and retrieving session variables (PHP 5.1.2)

  • Session variables were being saved, but not restored. Username Session variable KT_Username(MM_Username) - Dreamweaver MX and UltraDev Zone - DMXzone.COM provided me the solution. Php.ini defaulted set session.use_trans_sid to '0'. I could changing it to '1' with ini_set('session.use_trans_sid', 1); which seemed to resolve the problem, however, this method automatically adds the SID to all of the website's links. In order to overcome that, if found this seems to works
    1. To alter php.ini setting where you don't have access to the file, hard code the following values: ini_set('session.use_trans_sid', 1); in case your server's php.ini is set differently
      ini_set('session.use_cookies', 0); in case your server's php.ini is set differently
      ini_set("register_globals", 0);
      ini_set('session.auto_start', 0);
      ini_set("url_rewriter.tags", "a=href,area=href,frame=src,input=src,fieldset=");
      ini_set("arg_separator.output", "&");
      then;
    2. session_start();
    3. For some reason or other, placing <input type="hidden" name="PHPSESSID" value="<?php echo session_id(); ?>" /> within <fieldset></fieldset> tags solved my problems. - See [And Breathe Out]
      • Critical Note!!!!name="Phpsessid" would not work where name="PHPSESSID" did. It should also be noted that using session_name('Somesuch') may screw things up, so it should be avoided unless you're really good at coding.

 

 

Php Lessons Learned ~ Continued below ]

Print Content || Go to Site Directory || Up || Dwn  
Niagara Original - Sustainability

 

Website created & Managed by

For Legacies' Sake


  Email us at SN:

Email Address:

Name:


xhtml  ||  css ||  php notes || test links

Objective 5: Local debugging in PHPEclipse 1.1.8 in Eclipse 3.1.2 for PHP 5.1.2 using the DBG debugger

  • Observation 1:The appropriate php interpreter has to be php-cgi.exe and not php.exe
  • Observation 2:The debugger generates different $_SERVER variable than otherwise anticipated. In order for my script to run properly, I needed $_SERVER["HTTP_HOST"] which was not initialized within the debugger, nor was $_SERVER["HTTP_USER_AGENT"] and while the script was anticipating something like /folder/subfolder/page.php for $_SERVER["PHP_SELF"] the debugger set PHP_SELF to something like c:\\directory\\subdir\\folder\\subfolder\\page.php. I could override HTTP_HOST and HTTP_USER_AGENT by presetting them under Environment variables prior to initiating the debugger. Presetting PHP_SELF was of no use. The only way I could override the debugger setting was by setting a breakpoint immediately after the PHP_SELF variable was accessed, and then selecting the appropriate expression under the Variables tab of the debugger, and changing its value.

 


Objective 6: Fix <?=$SomeVariable?> so that it works for my online webpages.

  • I tried to using the ini_set('short_open_tag', 'On') which didn't correct the problem. Turns out, according to my research, since there are cases where the remote server still won't accept the open tag format. So, I had to search and replace <?= with <?php echo_ and ?> with ; ?>.

 


Objective 7: Resolve and Fix WARNING: include(../../include-folder/somefile.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in line ... followed by a string of other warning errors such as: WARNING: include() [<a href='function.include'>function.include</a>]: Failed opening '../../includes-folder/somefile.php' for inclusion (include_path='.:/usr/local/php5/lib/php') ... . especially annoying since the offending file seems ok.

  • One reason lies in the URI containing containing more than one forward slash such as http://www.website.com//subdirectory/webpage.php. To correct, use ltrim($_SERVER['PHP_SELF'], '/') and redirect back to the rectified URI. Cautionarly Note! Make sure the redirect hasn't inadvertanttly duplicated the first subdirectory as in the example above. Redirect to the absolute and not the relative address.
  • Another is the result of using <meta http-equiv="refresh" content="1;URL=gosomewhereelse.ext" /> rather than the php function header("Location: gosomewhereelse.ext"); exit(); -- NOTE! The header function has to appear before anything else is written to the file, or else you get a 'header already called' error.

 

Print Content || Go to Site Directory || Up || Dwn  

Objective 8: Highlight strings not within tags

  • Observation: I could not find a regular expression to easily search and replace strings not contained within anchors, or other tags. Searching the net yielded nothing other than a post at http://www.webmasterworld.com/forum88/1212.htm which gave me the idea for the HiliteSpecial ($haystack, $search, $replace=false, $tags=false) function below.

    NOTE!
    • $tags is used for future expansion for when search and replace within tags will be needed.
    • $replace is set to false here, also for future purposes, but for now, the function call:

      $excerpt=HiliteSpecial($some_string, $lookforthis, "<span class=\"tobehighlighted\">$lookforthis</span>", true); ... established the replacement I needed
    • HiliteSpecial's regular expression identifies text strings outside of any tags. The preg_match_all gathers the strings into the $strings variable.
      • Observation A: Without '(int)' in it, the in_array() function didn't work as expected.
      • Observation B: I read that strpos() is faster than strstr(). There is a flaw in using it in a conditional statement, however. When I used it, it failed to recognize the $search value when it in fact started from '0'. (Damn! Now I have to go back and check my other scripts.)
          function HiliteSpecial ($haystack, $search, $replace=false, $tags=false) {
           if ($temp=preg_match_all('/(?<=^|>)[^><]+?(?=<|$)/', $haystack, $strings) && in_array((int) $search, $strings[0]) && $tags) {
            $strings=$strings[0];
            foreach ($strings as $count=>$var) {
             if (strstr($var, $search)) $haystack=str_replace($var, str_replace($search, $replace, $var), $haystack);
            }
            return $haystack;
           } else return preg_replace("/$search/i", $replace, $haystack);
          } // end HiliteSpecial
          

 

Print Content || Go to Site Directory || Up || Dwn  

 

Objective 9: Determine what causes Error in my_thread_global_end(): 1 threads didn't exit\r. Internet searches on what might be causing this, and how to fix it, at first seemed futile. Eventually at PHP Bug #41350, I did find what I needed to seemingly resolve it. (See Observation B:, below)

  • Observation A: I had discovered that the error resulted from within the script below:
            1 ob_start();
            2 $string = eval("?>".$string);
            3 $string = ob_get_contents();
            4 ob_end_clean();
          
    The error occurred on line 2 since the string being evaluated contained "<?php if (!$ThisVariable) { ?> some normal text" but the closing tag (<?php } ?>) was missing. Once I resovled that problem, I stopped getting the
  • Observation B: I had recently integrated CURL in with PHP 5.2.4. A test I ran proved the error unrelated to MySQL. Still, the solution for me was to download the libmysql.dll, unzip it and overwrite the libmysql.dll existing in the PHP program folder.

 

Print Content || Go to Site Directory || Up || Dwn  
 
Please consider emailing in your feedback for Niagara's Sustainability.
 
Top of Page  |  Home  | Sitemap  | About  | Niagara  | SN Tables  | Resources  | Stats  | Musings  | Feedback
Disclaimer: The information provided through Sustainable Niagara, Inc. is without charge as a convenience to visitors. Any reference to products, services, links and other information does not constitute recommendation, endorsement or sponsorship. Nor does it particularly reflect the views and/or opinions of Sustainable Niagara, as an organization. We apologize for any links which may have become inactive over time.