PHP: Deprecated: The ereg extension is deprecated and will be removed in the future:

What can you do?
replace ereg() with @ereg()
or
set error reporting in php.ini to error_reporting = E_ALL ^ E_DEPRECATED
to eliminate the messages, but eventually you will have to port
or
port to preg_match()

you should know 4 main things to port ereg patterns to preg_match:

1. Add delimiters(/): ‘pattern’ => ‘/pattern/’

ereg(‘A-Z0-9a-z’,’string’); becomes preg_match(‘/A-Z0-9a-z/’,’string’);

eregi(‘pattern’,’string’); becomes preg_match (‘/pattern/i’,’string’);

2. Escape delimiter if it is a part of the pattern: ‘patt/ern’ => ‘/patt\/ern/’

Achieve it programmatically in following way:
$old_pattern = ‘<div>.+</div>’;
$new_pattern = ‘/’ . addcslashes($ereg_pattern, ‘/’) . ‘/’;

3. eregi(case-insensitive matching): ‘pattern’ => ‘/pattern/i’ So, if you are using eregi function for case insenstive matching, just add ‘i’ in the end of new pattern(‘/pattern/’).

4. ASCII values: In ereg, if you use number in the pattern, it is assumed that you are referring to the ASCII of a character. But in preg, number is not treated as ASCII value. So, if your pattern contain ASCII value in the ereg expression(for example: new line, tabs etc) then convert it to hexadecimal and prefix it with \x.

Example: 9(tab) becomes \x9 or alternatively use \t.

PHP: Deprecated: The mysql extension is deprecated and will be removed in the future:

gotta love this one, common PHP functions we have used for years are now going to be phased out, and we get warnings all over our pretty web page reminding us. How to fix

Option 1:
set error reporting in php.ini to
error_reporting = E_ALL ^ E_DEPRECATED

Option 2:
remove the warning by adding a ‘@’ before the function
such as:
$connect = @mysql_connect(‘localhost’,’root’,”);
or
ereg

Option 3:
Update your code to use the NEW functions (watch it, they changed around syntax on some
example:

$query = “Here the query you are going to perform”;

//QUERY PHP 5.4 o earlier (DEPRECATED)
$result = mysql_query ($query) or exit(“The query could not be performed”);

//QUERY PHP 5.5 (NEW EXTENSION)
$result = mysqli_query ($query) or exit(“The query could not be performed”);

note: ereg() has been replaced with preg_match()

XAMP – PHP – Fatal error: Call to undefined function mysql_connect()

Just installed the latest XAMP from ApacheFriends.org, and received this error.

WTF?

To fix, edit php.ini which is in XAMP home /php

unremmark  (remove the two ;;) from the line that contains ;;extension=php_mysql.dll

Now while I appreciate the work done on this free project, AMP does stand for Apache PHP-MYSQL,

one would assume that functionality would be “out of the box”. – silly me

$this in PHP5 breaks

$this in PHP5

In PHP4 anything can be assigned to the variable $this. But not anymore in PHP5.

Here is a code snipped from dracko
include_once($_SERVER[‘DOCUMENT_ROOT’]."/libs/drackoManage.php");
$this->dracko=new drackoManage();

Works fine in PHP4, and PHP5 stops with a fatal error: "Using $this when not in object context".

Ways to fix: rewrite your code to use another variable name than ‘this’ such as $_this.

When you call a static function in a Php class, it has the side-effect of wiping out the class context. Without the class context, you can’t make other calls to class members. In my case above, Php allowed me to break the class context (calling a non-static as static) without warning, which caused a perfectly legitimate class member function fail:

Instead, Php should warn: Calling a non-static member as static. This might not be what you meant to do.

Set which PHP errors are reported

Set which PHP errors are reported

// Turn off all error reporting
error_reporting(0);

// turn on error reporting
ini_set(‘display_errors’,’On’);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings …)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (bitwise 63 may be used in PHP 3)
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set(‘error_reporting’, E_ALL);

Scripting a Dynamic PHP Copyright

One of the benefits of scripting the date in PHP is the viewer does not get to see the script, but only the html outcome. Try replacing Your copyright code for the following PHP script, substituting the starting year for your starting year! this will save you lots of time in the future!

Below is the PHP script for adding a dynamic, or automatically updating copyright date for your site. Copy and paste it into your page exactly where your copyright is now.

<?php echo \"Copyright © 1998-\".date(\’Y\’); ?>

PHP – How many are online

Here is the script we use at Dracko.com to show how many users are online

function ShowOnline(){
// Timeout – After this time the users will
// be deleted (in minutes)
$timer = 100;
$filename = $_SERVER[‘DOCUMENT_ROOT’]."/usersonline.log";

if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$ip = $_SERVER[‘REMOTE_ADDR’];
$string = "$ip|$time\n";
$a = fopen("$filename", "a+");
fputs($a, $string);
fclose($a);

$timeout = time()-(60*$timer);

$all = "";
$i = 0;
$datei = file($filename);
for ($num = 0; $num < count($datei); $num++) { $pieces = explode("|",$datei[$num]); if ($pieces[1] > $timeout) {
$all .= $pieces[0];
$all .= ",";
}
$i++;
}

$all = substr($all,0,strlen($all)-1);
$arraypieces = explode(",",$all);
$useronline = count(array_flip(array_flip($arraypieces)));
$whoonline = array_flip(array_flip($arraypieces));

// Delete older that $timer*60
$dell = "";
for ($numm = 0; $numm < count($datei); $numm++) { $tiles = explode("|",$datei[$numm]); if ($tiles[1] > $timeout) {
$dell .= "$tiles[0]|$tiles[1]";
}
}

if (!$datei) $datei = dirname(__FILE__)."/$filename";
$string = "$dell";
$a = fopen("$filename", "w+");
fputs($a, $string);
fclose($a);
// display how many people where activ within $timeout
echo "Online:".$useronline;
if($this->user->isLoggedIn() && $this->user->isAdmin()){
$result = array_unique(explode(",",$all)); // get rid of dups(returns array of string $all)
asort($result);
foreach ($result as $value) {
echo "
$value";
if(strcmp($value,$_SERVER[‘REMOTE_ADDR’]) == 0){
echo "*";
}
}
echo "
last ".$timer." minutes";
}
}

Reading a file’s contents with PHP

PHP provides three built-in functions which allow you to easily read the contents of a file on your webserver. This is useful when, for example, another program may write information to the file and you could access that information through your script.

In this example, we’re going to use the following example 5 line file, saved as file.txt:

line 1
line 2
line 3
line 4
line 5

In the same directory as file.txt, we’re going to work with the PHP file test.php. We’ll outline the file reading functions below.
readfile()

readfile() is the least useful of the file functions. As a parameter, it takes a string which specifies the location of the file to read – this can be relative or absolute; since our file is the same directory as the script, we just need to pass the string ‘file.txt’ as this paramter. We call the function using the code readfile(‘file.txt’) and it prints out the contents of the file straight to the browser:
Click here to view the below code as plain text
PHP:

1.
// readfile() writes the contents of the file straight to the browser
2.
echo ‘Contents of file.txt using readfile():
‘;
3.
readfile(‘file.txt’);

The disadvantage of readfile() is that it doesn’t allow you to manipulate the file contents before displaying it – the next two functions we’re going to look at will allow you to do that.
file()

The code file(‘file.txt’) will read the contents of file.txt into an array, which you can then manipulate in your scripts and display yourself. Each line of the file is stored in a seperate element of the array, this means that we can access and manipulate each line of the file seperately using normal array notation.
Click here to view the below code as plain text
PHP:

1.
// file() loads the contents of file.txt into an array, $lines
2.
// each line in the file becomes a seperate element of the array.
3.
$lines = file(‘file.txt’);
4.

5.
// now loop through the array to print the contents of the file
6.
echo ‘Contents of file.txt using file():
‘;
7.
foreach ($lines as $line)
8.
{
9.
echo htmlspecialchars($line) . ‘
‘;
10.
}
11.

12.
// we can also access each line of the file seperately
13.
echo ‘3rd line of the file: "’ . htmlspecialchars($lines[2]) . ‘"
‘;

In this example, we’ve loaded the contents of file.txt into an array called $lines then used foreach to loop through the array and display each line on the user’s browser. We then use $lines[2] to access the third line of the file (the element at array index 2, since line 1 is $lines[0]).

You may notice that we’ve used a function called htmlspecialchars() when displaying the file’s contents – this enables that special characters used in HTML, such as > and " are displayed correctly. This illustrates the power of file() over readfile() since readfile() was unable to perform this kind of processing.
get_file_contents()

The last method we will look at is get_file_contents(‘file.txt’) which is similar to file() however rather than returning an array, it returns a string with all the lines of the file. We can manipulate and display this in a similar way as with file():
Click here to view the below code as plain text
PHP:

1.
// file_get_contents() reads the file and places the contents in a string
2.
$fileString = file_get_contents(‘file.txt’);
3.
echo ‘Contents of file.txt using file_get_contents():
‘;
4.
echo nl2br( htmlspecialchars($fileString) );

Since we have no way of seperating the lines with this method, we’ve used the nl2br() function which converts line breaks in the string (represented by the special character \n) into the HTML line break
so that the file will display correctly on the visitor’s browser.

The full contents of test.php is below:
Click here to view the below code as plain text
PHP:

1.
‘;
8.
foreach ($lines as $line)
9.
{
10.
echo htmlspecialchars($line) . ‘
‘;
11.
}
12.

13.
// we can also access each line of the file seperately
14.
echo ‘3rd line of the file: "’ . htmlspecialchars($lines[2]) . ‘"
‘;
15.
echo ‘
‘;
16.

17.
// file_get_contents() reads the file and places the contents in a string
18.
$fileString = file_get_contents(‘file.txt’);
19.
echo ‘Contents of file.txt using file_get_contents():
‘;
20.
echo nl2br( htmlspecialchars($fileString) );
21.
echo ‘

‘;
22.

23.
// readfile() writes the contents of the file straight to the browser
24.
echo ‘Contents of file.txt using readfile():
‘;
25.
readfile(‘file.txt’);
26.
?>

The output from test.php is:

Contents of file.txt using file():
line 1
line 2
line 3
line 4
line 5
3rd line of the file: "line 3 "

Contents of file.txt using file_get_contents():
line 1
line 2
line 3
line 4
line 5

Contents of file.txt using readfile():
line 1 line 2 line 3 line 4 line 5