Tag Archives: PHP

PHP: What’s a valid JavaScript identifier (or function name)?

After another reply to a question I’ve had on StackOverflow for a while, I decided that I perhaps should add another level of security to my method of providing JSONP from PHP. The way I did it before, I didn’t do any checking on the provided callback. This means that someone could technically put whatever they wanted in there, including malicious code. So, therefore it might be a good idea to check if the callback, which should be a function name, actually is a valid function name. But,

Continue reading PHP: What’s a valid JavaScript identifier (or function name)?

PHP Tutorial: PayPal Instant Payment Notification (IPN)

Got a letterIn a previous post I tried to give an introduction on how to get started with PayPal Payment Data Transfers (PDT). PDT is very handy in several cases, but you can’t always rely on it since it requires the user to return to your page after doing the payment. That will often happen, but it’s not guaranteed to happen. If you for example want to mark an order in your system as paid or something like that, you most likely want to use PayPal Instant Payment Notifications (IPN) in addition to PDT.

Instant Payment Notification (IPN) is a message service that notifies you of events related to PayPal transactions. You can use it to automate back-office and administrative functions, such as fulfilling orders, tracking customers, and providing status and other information related to a transaction. — PayPal

Once again the documentation, tutorials and code samples I found on this was a bit all over the place. Sort of messy and outdated. So, once again I decided to do my own thing and just follow the steps required and implement them myself. And since the tutorial on PDT turned out to be a bit of a success, I decided to share this too. Hopefully it can make the lives of fellow developers easier 🙂

Continue reading PHP Tutorial: PayPal Instant Payment Notification (IPN)

PHP: Tail tackling large files

Needed a function that could get me the last N lines of a log file. Wanted it to be efficient and not dependent on anything other than my code.

Found some versions, but they were either a bit messy or depended on unstable arithmetic (where filesize is greater than PHP_INT_MAX). So, I decided to take on the challenge and try to write one myself. Nice little exercise 🙂

Continue reading PHP: Tail tackling large files

PHP: How to get all images from an HTML page

I was curious to how I could make something similar to what Facebook does when you add a link. Somehow it loads images found on the page your link leads to, and then it presents them to you so you can select one you want to use as a thumbnail.

Well, step one to solve this is of course to find all the images on a page, and that is what I will present in this post. It will be sort of like a backend service we can use later from an AJAX call. You post it a URL, and you get all the image URLs it found back. Let’s put the petal to medal!

Continue reading PHP: How to get all images from an HTML page

PHP: Dealing with absolute and relative URLs

I’m currently writing a post on how to get image tags from a remote HTML page using PHP. One sticky issue with that is that the image URLs you find is a joyful mix of absolute and relative URLs.

Luckily, I found a function on nashruddin.com which seem to handle them alright. After a bit of clean up and fixing an error, we have this function:

function make_absolute($url, $base)
    // Return base if no url
    if( ! $url) return $base;

    // Return if already absolute URL
    if(parse_url($url, PHP_URL_SCHEME) != '') return $url;
    // Urls only containing query or anchor
    if($url[0] == '#' || $url[0] == '?') return $base.$url;
    // Parse base URL and convert to local variables: $scheme, $host, $path

    // If no path, use /
    if( ! isset($path)) $path = '/';
    // Remove non-directory element from path
    $path = preg_replace('#/[^/]*$#', '', $path);
    // Destroy path if relative url points to root
    if($url[0] == '/') $path = '';
    // Dirty absolute URL
    $abs = "$host$path/$url";
    // Replace '//' or '/./' or '/foo/../' with '/'
    $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
    for($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {}
    // Absolute URL is ready!
    return $scheme.'://'.$abs;

I can sort of read through and see what it does, but I can’t explain it very well. So, I’ll just leave it at that. So far it has worked fine for me. Maybe some corner cases that are missing, and if there are, please let me know!

💡 What I added to the original function was line 5 and 17. The first to prevent it from crashing if the url is null or empty, and the second to prevent it from crashing if parse_url finds no path. For example if the url is http://www.example.com (No /whatever at the end).

The base tag

A tag that is easy to forget about is the base tag. The above function gets the base path from the URL given as base. For example if you gave it http://www.example.com/directory/file.html as base, it would use http://www.example.com/directory/. However, if file.html included the following base tag:

<base href="http://www.example.com/">

Then the base path would be http://www.example.com/ instead. Fun, eh?

As long as you know about it, it’s not to hard to deal with though. You just need to get a hold of it and provide that as base instead when using the function above.

Works On My Machine™! And if it doesn’t on yours, let me know. If it’s a mistake in the function, I’d like to fix it!