May 20, 2011

Updating _SESSION variables from old PHP v4 script

Filed under: PHP Hints and Tricks — Paul Vint @ 12:08 pm

Quick way to update code that reads ‘session_is_registered(‘foo’) to isset($_SESSION['foo']) for PHP5 usage:
In vi type:
:%s/session_is_registered(\"\([a-zA-Z]*\"\))/\isset($_SESSION\[\'\1\'\])/

December 22, 2010

Porting PHP4 to PHP5 Gotcha

Filed under: PHP Hints and Tricks — Paul Vint @ 10:40 am

Came across a little “gotcha” porting a site from PHP 4.4.4 to version 5
if($a == $b)
{
$x = new whateverObject(); // note that this object may or may not get instantiated
}
.....
$x->doSomething(); // this will cause "Fatal error: Call to a member function doSomething() on a non-object
...

How this ever worked in PHP4, I don’t know… might have to check into what happens there.

November 9, 2010

PHP Variable as Array name (variable variablenames)

Filed under: PHP Hints and Tricks — Paul Vint @ 1:26 pm

To use a PHP variable as a name for an array:

$theArray = array("a","b","c");

$variableReference = "theArray"; // Note: no []

echo ${$variableReference}[0]; // outputs just like $theArray[0] - ie: "a"

Note that this is generally a bad idea, as it makes code brutally hard to read and debug, but it is handy sometimes.

August 29, 2010

Fade-in popup window example in Javascript

Filed under: Javascript & AJAX,PHP Hints and Tricks — Paul Vint @ 9:37 am

Source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<script type="text/javascript">// <![CDATA[
// Browser safe opacity handling function
function setOpacity( divid, value ) {  
 document.getElementById( divid ).style.opacity = value / 10;
 document.getElementById( divid ).style.filter = 'alpha(opacity=' + value * 10 + ')';
}
function fadeInMyPopup( divid ) {
  for( var i = 0 ; i <= 100 ; i++ )
  setTimeout( 'setOpacity("' + divid + '", ' + (i / 10) + ')' , 8 * i );
}
function fadeOutPopup( divid ) {
 for( var i = 0 ; i <= 100 ; i++ ) {
   setTimeout( 'setOpacity("' + divid + '", ' + (10 - i / 10) + ')' , 8 * i );
 }
 setTimeout('closeMyPopup( divid )', 800 );
}
function closeMyPopup( divid ) {
 document.getElementById( divid ).style.display = "none";
}
function launchMyPopup( divid ) {
 setOpacity( divid, 0 );
 document.getElementById( divid ).style.display = "block";
 fadeInMyPopup( divid );
}
// ]]></script>
<div id="popup1" style="width: 380px; height: 300px; display: none; position: absolute; top: 130px; left: 80px;">
<table border="0" cellspacing="0" cellpadding="0" width="380">
<tbody>
<tr>
<td><img src="/images/menubar_bg.png" alt="" width="356" height="23" /></td>
<td><a href="javascript:fadeOutPopup(&quot;popup1&quot;);"><img src="/images/window-close.png" border="0" alt="" width="24" height="23" /></a></td>
</tr>
<tr>
<td style="padding: 5px 10px; vertical-align: text-top; background: url(/images/popup_body.png) no-repeat scroll left top transparent; width: 380px; height: 277px;" colspan="2">
This is a test of the emergency pop up system.
If this were an actual emergency, you'd have been trampled. :-)</td>
</tr>
</tbody>
</table>
</div>
<input onclick="launchMyPopup('popup1')" type="submit" value=" Launch Popup1! " />

You can have multiple popups on one page simply by changing the DIV id and setting the calls to fadeInPopup() and launchMyPopup() accordingly.

1 person likes this post.

August 9, 2010

Fixing “Warning: file_get_contents()” php error

Filed under: PHP Hints and Tricks — Paul Vint @ 7:31 pm

I recently got the following error on a newly installed webserver:

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /var/www/localhost/htdocs/wordpress/wp-content/plugins/statsurfer/statsurfer.php on line 3543
This happened to me after activating StatsSurfer for this blog, and that big ugly error would appear at the top of every page.

Thankfully, the fix is quite easy (strangely hard to google for though), in your php.ini file (mine’s at /etc/php/apache2-php5/php.ini – other *nix variants will be similar), just change the following line in the php.ini file from:

allow_url_fopen = Off

To:

allow_url_fopen = On

I recently got the following error on a newly installed webserver:

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /var/www/localhost/htdocs/wordpress/wp-content/plugins/statsurfer/statsurfer.php on line 3543
This happened to me after activating StatsSurfer for this blog, and that big ugly error would appear at the top of every page.

Thankfully, the fix is quite easy (strangely hard to google for though), in your php.ini file (mine’s at /etc/php/apache2-php5/php.ini – other *nix variants will be similar), just change the following line in the php.ini file from:

allow_url_fopen = Off

To:

allow_url_fopen = On

Restart Apache, and off you go.

2 people like this post.

Powered by WordPress