Simon Willison’s Weblog

Subscribe

80 items tagged “php”

2024

ActivityPub Server in a Single PHP File (via) Terence Eden: “Any computer program can be designed to run from a single file if you architect it wrong enough!”

I love this as a clear, easy-to-follow example of the core implementation details of the ActivityPub protocol—and a reminder that often a single PHP file is all you need. # 19th February 2024, 12:20 am

Making a Discord bot with PHP (via) Building bots for Discord used to require a long-running process that stayed connected, but a more recent change introduced slash commands via webhooks, making it much easier to write a bot that is backed by a simple request/response HTTP endpoint. Stuart Langridge explores how to build these in PHP here, but the same pattern in Python should be quite straight-forward. # 14th January 2024, 10:56 pm

2023

hubcap.php (via) This PHP script by Dave Hulbert delights me. It’s 24 lines of code that takes a specified goal, then calls my LLM utility on a loop to request the next shell command to execute in order to reach that goal... and pipes the output straight into exec() after a 3s wait so the user can panic and hit Ctrl+C if it’s about to do something dangerous! # 6th September 2023, 3:45 pm

2021

PHP 8.1 release notes (via) PHP is gaining “Fibers” for lightweight cooperative concurrency—very similar to Python asyncio. Interestingly you don’t need to use separate syntax like “await fn()” to call them—calls to non-blocking functions are visually indistinguishable from calls to blocking functions. Considering how much additional library complexity has emerged in Python world from having these two different colours of functions it’s noteworthy that PHP has chosen to go in a different direction here. # 25th November 2021, 7:53 pm

htmlspecialchars was a very early function. Back when PHP had less than 100 functions and the function hashing mechanism was strlen(). In order to get a nice hash distribution of function names across the various function name lengths names were picked specifically to make them fit into a specific length bucket. This was circa late 1994 when PHP was a tool just for my own personal use and I wasn’t too worried about not being able to remember the few function names.

Rasmus Lerdorf # 22nd November 2021, 7:23 pm

2013

Is greater comfort with Windows a good enough reason to switch from PHP to ASP.NET?

Learning Linux really isn’t that hard, and it will dramatically increase your potential horizons as a programmer. Install Ubuntu on a virtual machine on your laptop and start running through some tutorials.

[... 53 words]

How log in my e-mail and send sms via php?

If you want to send SMS via PHP I suggest taking a look at www.twilio.com—they are pretty inexpensive and ridiculously easy to get started with.

[... 44 words]

What is the best framework to use, Yii or Ruby on Rails?

This is a big decision, and it’s worth taking the time to pick what’s the best fit for you. I recommend going through the tutorials for each one, building the basic application they describe and seeing which made the most sense to you. As a Django developer, I suggest trying that framework too :)

[... 126 words]

How can I get access to the PHP script of websites like Dropbox?

If a website doesn’t deliberately publish its server-side code (some sites like reddit do this, but it’s pretty rare) then you won’t be able to see it. You can search for an open source clone but these will often be pretty low quality—the smartest open source developers tend to work on libraries that solve common problems rather than putting their efforts in to building complete clones of existing sites.

[... 175 words]

Does creating a zip file with php use more RAM then CPU? Or vise versa? Also, is it faster to use a system call, or the php zip library? Exec (zip filename.zip) vs. $zip =new ZipArchive().

You can find out the answer yourself using a very simple benchmark—just call time() before and after each option and loop them a few thousand times to calculate an average.

[... 74 words]

Which should I learn: Python or PHP?

Python will teach you more about programming than PHP—and you’ll be able to learn PHP easily if you learn Python first.

[... 36 words]

2012

System Administration: What service/product do you recommend for central logging of events and errors from multiple servers? Why?

We rolled our own solution to this using MongoDB, due to its super-fast writes and ability to store, index and search JSON. We were also attracted by its capped collections, which make it easy to e.g. only log the last 100,000 items.

[... 113 words]

How can you build a search engine for a website built in PHP/MySQL?

There are a bunch of options.

[... 310 words]

What are XML feed best practices?

It sounds like you’re pretty much screwed already, if you’re dealing with companies that still think FTPing XML around is a sensible thing to do.

[... 364 words]

Is there a framework that allows me to collect input from individual users, and then charge for the aggregate and analysis of that data?

No—your needs are extremely specific. You’re going to have to build this yourself.

[... 95 words]

What are some good examples of a first PHP app to build for someone who is learning?

Build a blog. Blog engines are, in my opinion, the ideal starter project for learning any server-side web technology.

[... 172 words]

2011

Can someone improve their knowledge of programming in Ruby or PHP by using a framework like Rails or Zend, or does the framework just do a lot of the work for you without giving you an opportunity to learn from it?

Read the source, luke.

[... 82 words]

How are real time web applications achievable with PHP?

You don’t need to build your comet server using the same technology as the rest of your site.

[... 181 words]

2010

What are the tradeoffs (e.g. development speed, performance, scalability) between using various php frameworks, ruby/rails, or python/django?  Is there any reason to choose one overwhelmingly over another?

At this point, I’d argue that the decision between them comes down to programming language rather than framework—the frameworks have mostly converged on a very similar set of features.

[... 145 words]

In what circumstances should one use “magic quotes” in PHP?

Absolutely never. Magic quotes was a badly designed feature, and PHP has been trying to escape its legacy for years. If you are constructing SQL strings using string concatenation you’re asking for trouble—use prepared statements or a library that interpolates and correctly escapes variables for you.

[... 65 words]

Hookbox (via) For most web projects, I believe implementing any real-time comet features on a separate stack from the rest of the application makes sense—keep using Rails, Django or PHP for the bulk of the application logic, and offload any WebSocket or Comet requests to a separate stack built on top of something like Node.js, Twisted, EventMachine or Jetty. Hookbox is the best example of that philosophy I’ve yet seen—it’s a Comet server that makes WebHook requests back to your regular application stack to check if a user has permission to publish or subscribe to a given channel. “The key insight is that all application development with hookbox happens either in JavaScript or in the native language of the web application itself”. # 29th July 2010, 9:48 am

The Onion Uses Django, And Why It Matters To Us. The Onion ported their main site from PHP and Drupal to Django in three months with a team of four developers, including a full migration of their archived content. Their developers answer questions about the switch in this thread on the Django sub-reddit. # 25th March 2010, 6:43 pm

HipHop for PHP: Move Fast. Facebook have open-sourced their internally developed PHP to C++ compiler. They serve 400 billion PHP pages a month (that’s more than 150,000 a second) so any performance improvement dramatically reduces their hardware costs, and HipHop drops the CPU usage on their web servers by an average of 50%. “We are serving over 90% of our Web traffic using HipHop, all only six months after deployment”. # 2nd February 2010, 6:59 pm

2009

Drupal or Django? A Guide for Decision Makers. A surprisingly interesting comparison—the author describes Django as “a framework with CMS-like tendencies” and Drupal as “a CMS with framework-like tendencies”, then explores the benefits of those two different approaches. # 15th November 2009, 10:14 pm

nginx_http_push_module. More clever design with webhooks—here’s an nginx module that provides a comet endpoint URL which will hang until a back end process POSTs to another URL on the same server. This makes it much easier to build asynchronous comet apps using regular synchronous web frameworks such as Django, PHP and Rails. # 17th October 2009, 4:48 pm

Scriptlets—Quick web scripts (via) From the prolific Jeff Lindsay, a pastebin-style tool for short server-side scripts written in Python, JavaScript or PHP that executes them within a Google App Engine powered sandbox. The Java code that implements the service is available on GitHub. # 13th August 2009, 1:51 pm

djng—a Django powered microframework

djng is nearly two weeks old now, so it’s about time I wrote a bit about the project.

[... 1501 words]

Cross Browser Base64 Encoded Images Embedded in HTML (via) Scarily clever. View the PHP source to see what’s going on—most browsers get image tags that use data URIs starting with data:image/png;base64, but IE gets served a Content-type:message/rfc822 header and a MIME formatted multipart/related document, as used by e-mail clients to embed inline image attachments. # 17th April 2009, 4:12 pm

Browsing my browsing. Roo Reynolds used the MeeTimer Firefox extension to gather statistics on his browsing habits, then extracted data directly from the SQLite database and generated his own graphs using PHP and the canvas element. # 10th April 2009, 8:48 am

Database Sharding at Netlog, with MySQL and PHP. Detailed MySQL sharding case study from Netlog, who serve five billion page requests a month using thousands of shards across more than 80 database servers. # 2nd March 2009, 10:22 am