Archive

Archive for December, 2008

How to Parse All Links From a Page With PHP Using DOM Technology

December 17th, 2008 No comments

Today I will share a simple script that may be used to extract all URLs from a single page. You don’t have to deal with regular expressions anymore, if you don’t like them. DOM technology that is integrated in PHP5 allows you to do this in just some strings of your code without any specific knowledge. Here is the solution.

<?php
error_reporting(0);
$url=”http://mail.ru”;
$content=file_get_contents($url);
$dom = new DOMDocument;
if ($dom->loadHTML($content))
{
$as = $dom->getElementsByTagName(“a”);
foreach ($as as $a)
{
$allurl.=$a->getAttribute(“href”).”\r\n”;
}
}

echo $allurl;
?>

Everything is quite simple and I don’t think there are any explanations necessary. The only limitation is that you need to have a valid enough HTML code on the page you’re parsing. The script above will collect all links from a single page by href attribute of <a> tag. If you can do this simpler, you’re welcome to show me the solution.

How to Enable Recovery Console in Microsoft Windows XP

December 14th, 2008 1 comment

Today I will create a less typical post that will be related to Microsoft Windows and especially to recovery console installation. On one of my PCs running Windows has appeared a message about the error in USER32.DLL file. I will post the solution here as there was a need to restore it from the original CD.

First of all we need to have an original CD with clean Windows installation. Then I have followed the information on Microsoft.com:

To install the Recovery Console, follow these steps:
Insert the Windows XP CD into the CD-ROM drive.
Click Start, and then click Run.
In the Open box, type d:\i386\winnt32.exe /cmdcons where d is the drive letter for the CD-ROM drive. In the case of ‘Microsoft Windows XP Professional x64 Edition, type d:\amd64\winnt32.exe /cmdcons where d is the drive letter for the CD-ROM drive.
A Windows Setup Dialog Box appears. The Windows Setup Dialog Box describes the Recovery Console option. To confirm the installation, click Yes.
Restart the computer. The next time that you start your computer, “Microsoft Windows Recovery Console” appears on the startup menu.

Then we’ll see the Recovery console after reboot. You’ll need to select it in order to recover any of the system files.  Then we need to persorm the following steps:

At the command prompt, type the following command:

cd %systemroot%\system32

First, rename the damaged or corrupted file so that it is not deleted when you copy the original file. To do this, type the following command:

ren USER32.DLL USER32.BAK

Next, restore the original User32.dll file from the Windows CD to your computer. To do this, type the following command:

expand [CD drive letter]:\i386\USER32.DL_ %systemroot%\system32 /Y

Note In this command, replace [CD drive letter] with the letter of your CD drive, such as D. If your computer does not recognize  %systemroot%, you’ll have to type in the full path manually (usually C:\Windows)

To exit the Recovery Console and to restart the computer, type exit at the command prompt, and then press ENTER. Your file will be restored after reboot.

The second part of article was created using the information found here. If you know any other methods that could help restoring system files in Windows, I’d suggest you to leave your comments.

How to Enable Socket Support In PHP

December 6th, 2008 1 comment

In order to add socket support with PHP you need jsut to include it into your configure string: there is no need to install any third-party applications or software. All you need is to recompile PHP, and it can be done in some minutes.

First of all you need to know where your PHP installation is located. I think you already have it installed if you are going to add some functions. Try searching for your PHP package (I mean installing PHP from source in this article) at your server: it should exist.

When you have the package near and know where it is located, you should identify your current configuration to avoid deleting some elements from the configure line. The most useful method is to copy your current configure line from the output produced by phpinfo(); You may create a new file containing just these lines:

<?
phpinfo();
?>

Then run it using a web browser. At the beginning of this output you should see a configure line: it should look like:

‘./configure’ ‘–with-apxs2′ ‘–with-curl=/usr/local/lib’ ‘–with-gd’ ‘–enable-gd-native-ttf’ ‘–with-ttf’ ‘–with-gettext’ ‘–with-jpeg-dir=/usr/local/lib’ ‘–with-freetype-dir=/usr/local/lib’ ‘–with-kerberos’ ‘–with-openssl’ ‘–with-mcrypt’ ‘–with-mhash’ ‘–with-mysql=/usr’ ‘–with-mysqli=/usr/bin/mysql_config’ ‘–with-pear’ ‘–with-png-dir=/usr/local/lib’ ‘–with-zlib’ ‘–with-zlib-dir=/usr/local/lib’ ‘–enable-zip’ ‘–with-iconv=/usr/local’ ‘–enable-bcmath’ ‘–enable-calendar’ ‘–enable-ftp’ ‘–enable-magic-quotes’ ‘–enable-mbstring’ ‘–enable-pcntl’

Copy it to your text editor and remove quotes and apostrophes. Then add ‘–enable-sockets’ to this line: that’s the only string we need to enable socket support.

Then we need to make PHP. If you’re doing it for the first time, see my previous post: How to configure PHP .

The command is simple enough: make :) . I’d suggest you to make clean before doing make to avoid some possible errors. After the PHP is  “made” you need to make install and then to reboot Apache or any web server you might have to see the changes.  If everything went OK, you should see that socket functions are now available.

How to Open a Javascript Pop Up With Fixed Size

December 1st, 2008 No comments

Modern browsers in their default settings are blocking pop up windows. A simple window.open will not open a pop-up as most probably it will be blocked. But what to do if you need to show a special offer or something like this?

I’d suggest you to open a window on user action, e.g. clicking a button or moving mouse. I’ve copied this script from the Internet and successfully using it to enable pop-ups on clicking the links.

<script type=”text/javascript” language=”javascript”>
<!– //
var popWin = null    // use this when referring to pop-up window
var winCount = 0
var winName = “popWin”
function openPopWin(winURL, winWidth, winHeight, winFeatures, winLeft, winTop){
var d_winLeft = 20  // default, pixels from screen left to window left
var d_winTop = 20   // default, pixels from screen top to window top
winName = “popWin” + winCount++ //unique name for each pop-up window
closePopWin()           // close any previously opened pop-up window
if (openPopWin.arguments.length >= 4)  // any additional features?
winFeatures = “,” + winFeatures
else
winFeatures = “”
if (openPopWin.arguments.length == 6)  // location specified
winFeatures += getLocation(winWidth, winHeight, winLeft, winTop)
else
winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop)
popWin = window.open(winURL, winName, “width=” + winWidth
+ “,height=” + winHeight + winFeatures)
}

function closePopWin(){    // close pop-up window if it is open
if (navigator.appName != “Microsoft Internet Explorer”
|| parseInt(navigator.appVersion) >=4) //do not close if early IE
if(popWin != null) if(!popWin.closed) popWin.close()
}

function getLocation(winWidth, winHeight, winLeft, winTop){
return “”
}

function getLocation(winWidth, winHeight, winLeft, winTop){
var winLocation = “”
if (winLeft < 0)
winLeft = screen.width – winWidth + winLeft
if (winTop < 0)
winTop = screen.height – winHeight + winTop
if (winTop == “cen”)
winTop = (screen.height – winHeight)/2 – 20
if (winLeft == “cen”)
winLeft = (screen.width – winWidth)/2
if (winLeft>0 & winTop>0)
winLocation =  “,screenX=” + winLeft + “,left=” + winLeft
+ “,screenY=” + winTop + “,top=” + winTop
else
winLocation = “”
return winLocation
}
//–>
// –>
</script>

Script usage is the following:

onClick=”javascript:openPopWin(‘Your URL’,Width,height,”,pixels from screen left to window left,pixels from screen top to window top)”

This will allow you to open a window and it is not blocked with browsers. If you have a more elegant solution, you’re welcome to post it here.