245681 items (663 unread) in 24 feeds
CNN
(6 unread)
MSNBC
(16 unread)
PHP
(57 unread)
Deals
(573 unread)
Tech
(3 unread)
Web Development
(1 unread)
CNN Money
(6 unread)
Frugal Blogs
(1 unread)
Truncated by Planet PHP, read more at the original (another 686 bytes)
create table clicks_new like clicks;
rename table clicks to clicks_2010032500001, clicks_new to clicks;
HTTP/1.1 200 OK
Date: Fri, 29 Jan 2010 15:30:34 GMT
Server: Apache
X-Powered-By: PHP/5.2.12-pl0-gentoo
Set-Cookie: WCSESSID=xxxxxxxxxxxxxxxxxxxxxxxxxxxx; expires=Sun, 28-Feb-2010 15:30:34 GMT; path=/
Content-Encoding: gzip
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
<!DOCTYPE html PUBLIC "-//W3C//DTD html 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Ramblings of a web guy</title>
.
.
So, all those lines before the html starts have to come first. HTTP headers are where things like cookies and redirection occur. When a PHP script starts to send html out to the browser, the headers are stopped and the body begins. When your code tries to set a cookie after this has started, you get the "headers already sent" error message.
<?php
if(DEV){
$server = "localhost";
} else {
$server = "10.1.1.25";
}
?>
; db.ini
[myconfig]
myconfig.db.mydb.db = mydb
myconfig.db.mydb.user = user
myconfig.db.mydb.pass = pass
myconfig.db.mydb.server = host
<?php
/**
* Creates a MySQLi instance using the settings from ini files
*
* @author Brian Moon
* @copyright 1997-Present dealnews.com, Inc.
*
*/
class MyDB {
/**
* Namespace for my settings in the ini file
*/
const INI_NAMESPACE = "dealnews";
/**
* Creates a MySQLi instance using the settings from ini files
*
* @param string $group The group of settings to load.
* @return object
*
*/
public static function init($group) {
static $dbs = array();
if(!is_string($group)) {
throw new Exception("Invalid group requested");
}
if(empty($dbs["group"])){
$prefix = MyDB::INI_NAMESPACE.".db.$group";
$db = get_cfg_var("$prefix.db");
 Truncated by Planet PHP, read more at the original (another 4107 bytes)
<?phpThat file needs to require two files, but each one is only needed by one function in the file. This is the dilemma we have found ourselves in. We have a file that is filled with functions for building, taking apart, repairing, fetching, or anything else you can think to do with a URL. So, at the top of that file are 13 require statements. This all happened organically over time. But, now we are in a situation where we load lots of files that may not even be needed to begin with. By moving to an autloading system, we will only include the code we need. This saves cycles and file IO.
require "DB.php";
require "Article.php";
function myfunc1() {
DB::somemethod();
}
function myfunc2(){
Article::somemethod();
}
?>
One place where people seem to stumble with PHP is with long running PHP processes or parallel processing. The pcntl extension gives you the ability to fork PHP processes and run lots of children like many other unix daemons might. We use this for various things. Most notably, we use it run Gearman worker processes. While at the OReilly Open Sourc Convention in 2009, we were asked about how we pulled this off. So, we are releasing the two scripts that handle the forking and some instructions on how we use them.
This is not a detailed post about long running PHP scripts. Maybe I can get to the dos and don'ts of that another time. But, these are the scripts we use to manage long running processes. They work great for us on Linux. They will not run on Windows at all. We also never had any trouble running them on Mac OS X.
The first script, prefork.php, is for forking a given function from a given file and running n children that will execute that function. There can be a startup function that is run before any forking begins and a shutdown function to run when all the children have died.
The second script, prefork_class.php, uses a class with defined methods instead of relying on the command line for function names. This script has the added benefit of having functions that can be run just before each fork and after each fork. This allows the parent process to farm work out to each child by changing the variables that will be present when the child starts up. This is the script we use for managing our Gearman workers. We have a class that controls how many workers are started and what functions they provide. I may release a generic class that does that soon. Right now it is tied to our code library structure pretty tightly.
We have also included two examples. They are simple, but do work to show you how the scripts work.
You can download the code from the dealnews.com developers' page.
Lots of people talk about scaling and performance. But, are they preparing for all the things that could happen? There are multiple problems and there is not one solution to solve them all.
Everything is running fine and BAM! – your site is linked from the front page of Yahoo! What do you do? How can you handle that sudden rush of traffic. Requests per second are running 5x normal levels. Servers have CPU spikes. Daemons are hitting the maximums. You are running out of bandwidth. How could you have been prepared for this? What are the tools and techniques for this type of sudden rush?
Or, lets say you have just come out of a meeting where everyone discovered that your site is growing in traffic 70% – 80% year over year. That means that 1 million page views this month will be nearly 3 million this time in 2 years. How can you plan for that? You don’t want to redesign the whole architecture every 2 years. What methods could be used to deal with this constant long term growth?
While there is no magic bullet for either of these scenarios, there are techniques used by many sites out there to help you get through these situations. This session will cover some of these techniques and talk about their pros and cons.
I must admit, this if the first time since 2000 that I am a little intimidated to speak at a conference. The people that present and attend Velocity are so awesome. I just hope I don't disappoint.