Archive

Posts Tagged ‘php’

Restricetd access to a web-application with htaccess or php

July 3rd, 2009

If you building a simple web-application and you are not really sure, ow secure are the authentication and or the rest of the application you should think about to limit the access to the application.
Furthermore, may your application should be available only for testing purpose of several customers the following could help you:

First way… using a .htaccess which controls the access using a apache web server.
This is a very fast sollution BUT you CAN’T use hostnames and or full qualified dns names like skycube.net or yourname.dyndns.org

<Limit GET HEAD POST>
order deny,allow
allow from 62.75.185.219
allow from 127.0.1.1
allow from 127.0.0.1
deny from all
</Limit>

The second way… which I prefer, using a php code snippet. This allows you to use hostnames and full qualified domain names. Definitely you can write it in less than the lines I used below for the code, but in of understanding what the code does, it’s better ;)

<?php
/**
 * ACL array
 */
$valid_hosts = array();
$valid_hosts[] = 'localhost';
$valid_hosts[] = '127.0.0.1';
$valid_hosts[] = '127.0.1.1';
$valid_hosts[] = 'yourname.dyndns.org';
$valid_hosts[] = 'skycube.net';
 
for($i=0;$i<sizeof($valid_hosts);$i++){
  $valid_ip = gethostbyname($valid_hosts[$i]);
  if($valid_ip == $_SERVER['REMOTE_ADDR']){
    $valid_state = 'valid';
    break;
  }
  else{
    $valid_state = 'invalid';
  }
}
if($valid_state != 'valid')
  header("Location: http://www.skycube.net");
?>

other, php , ,

Usefully phpMyAdmin config vars

June 16th, 2009

While working with large databases, phpMyAdmin is in serveral cases not the perfect application to handle changes and or administrate the databases but may the only one you are allowed to use.

In this order there are two default configuration the following to configuration vars will help you to work better and faster.

Show all rows without a limit:

$cfg['ShowAll'] = true;

Show full update statements:

$cfg['MaxCharactersInDisplayedSQL'] = 2147483647;

In order to the default value, updates with more than 1000 chars will be replaced with […].

Just place these in the config.inc.php e.g. /var/www/htdocs/phpmyadmin/config.inc.php

php ,

Session problems

February 21st, 2009

While you working with web applications it could happen that you get several errors.. Never underestimate real server problems. When you try to start a new Web 2.0 application or just the usage and/or visits of your application increase, maybe non-linear, you can login to your application but you can’t proceed.

On enterprise applications it’s recommended to set php display errors to off, so won’t see what really happened.

While tracking the problem to your browser and the customers and may the service provider, you should just login with a shell.
First thing you should do is, like all time, who and last. After that, may you try to find out what happen in the apache access and error log. You will find nothing, cause there is nothing critical, nothing new. May you see the session file in your filesystem, but why is it empty? A php bug?

Why? Ok just the answer, try this: df -h , you will see the a 100% full partition ;) and when you try a du -shm /var/log/* you see your problem… GB’s of apache logs…

What happened? Your Web 2.0 application uses polling, and your apache got a custom, combined log which writes to access.log.

  • Solution 1: Drop the line in your apache virtualhost configuration for custom, combined log
  • Solution 2: Proceed a massive logrotation
  • Solution 3: Pipe the combined log to a perl or php-cli script which filters the polling events (I prefer)

On local, staging and or development installations you should switch the display errors for php to on,… then you get messages like this:

WARNING: session_write_close() [function.session-write-close]...

Linux , , ,

Image to html via php

February 14th, 2009

Did you ever thought about to show an image on a website using a table?

The following script analyze the image and does a print out using a standard html table.

The CSS code:

1
2
3
4
<style type="text/css" media="screen">
.tableImage { padding: 0; margin: 0; border: 0; }
.tableImage TD { width: 1px; height: 1px; padding: 0; margin: 0; }
</style>

The php code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$image='sample.jpg';
$im = imagecreatefromjpeg($image);
$info=getimagesize($image);
$width=$info[0];
$height=$info[1];
for ($i=0; $i<$height; $i++){
	$html .= "<tr>";
	for ($j=0; $j<$width; $j++){
		$color_index = imagecolorat($im, $j, $i);
		$color_tran = imagecolorsforindex($im, $color_index);
		$html .= "<td style=\"background-color: rgb($color_tran[red],$color_tran[green],$color_tran[blue]);\"></td>\n";
	}
	$html .= "</tr>\n";
}
?>

Begin of the output table:

1
<table class="tableImage" cellpadding="0" cellspacing="0">

Include the generated image:

1
<?=$html ?>

End of output table:

1
</table>

Please note, that this could crash your web-server or browser, if you use to large images…

Projects , ,

ZMG-Project finally closed!

October 26th, 2008

Dear community,

the ZMG-Project is finally closed!

After long discussions, many ideas and hundreds of emails, we decided there is no way back… We can’t spend the time any longer and we have no youthful enthusiasm anymore to reanimate and produce a new stable version of ZMG for Joomla! 1.5.

May the force be with us ;)
Per Lasse Baasch for the ZMG-Team

Joomla!, Projects , , , , ,