From ASP to PHP

I recently decided to move all my future freelance and personal web sites to PHP and MySQL. I’ve never had any formal education in programming but have been using Classic ASP and MS SQL since 1999. I’d say I know my way around it pretty well by now. But Classic ASP is a sinking ship. I don’t want to be like one of those weirdoes that still develops Atari Jaguar games.

I chose PHP over ASP.NET because of two things: hosting costs and ease-of-use. My current Windows host charges $250/year for 1 domain and 1 MS SQL database. My new Apache host (Dreamhost) is charging me a little more than a quarter that price for unlimited domains and MySQL databases. Not too shabby. And the nice thing too, for a knows-only-enough-to-be-dangerous programmer like myself, there’s almost a one-to-one relationship between built in functions and operations from Classic to PHP. Design215 has a nice resource on those parallels.

I could have gone with Ruby On Rails but it turns out I’m not cool enough.

So, here are some of my observations as I start to learn PHP.

Functions

In Classic ASP, if you declare a function and call the same function on the same page, variables can be used between them. For example:

varFoo = 1

Function Bar()
Bar = varFoo + 1
End Function

Response.write (Bar())

This would return 2. But in PHP…

$varFoo = 1;

Function Bar() {
Return $varFoo + 1
}

Echo Bar();

This would return 1. Does this have something to do with heretofore theoretical scope? I’m sure Matt Groves can offer an explanation.

URL Rewriting

This is really nice. If you wanted “friendly urls” in Classic ASP with IIS, you needed to configure a custom 404 page that did the heavy lifting, and replace the request variables with session variables. With Apache, you just add a couple conditions to a “.htaccess” file and drop it into a directory. It can even be used to prevent image hotlinking.

Corz.org told me everything I needed to know.

Sessions

In Classic ASP, if you set a session variable on page x, and write it on page y, it goes something like this:

Page X

Session("foo") = "bar"

Page Y

Response.write(session("foo"))

Easy as pie. PHP requires a “session_start();” on every page that uses the session.

Page X

session_start();
$_SESSION["foo"] = "bar";

Page Y

session_start();
echo $_SESSION["foo"];

One hurdle I’ve encountered is with Dreamhost’s default PHP configuration (php.ini). Sessions are apparently set to never expire! I’m still trying to figure out if I can change it.

More to come…

Add a comment »8 comments to this article

  1. See http://www.evolt.org/article/Make_your_PHP_code_portable/17/28117/index.html for methods of overriding the php configuration.

    For a list of php.ini entries you can override, see http://us.php.net/manual/en/ini.php#ini.list

    Reply

  2. “I dont want to be one of those weirdos still developing Atari Jaguar Games” is easly one of the best lines ever said here

    Reply

  3. Regarding scope:
    In your example, the initial $varFoo is in the global scope, and the one within Bar() is in that function’s scope. You can reference variables in the global scope like so:

    $varFoo = 1;

    function Bar() {
    global $varFoo;
    return $varFoo + 1;
    }
    echo Bar();

    Reply

  4. Thanks, Correl. I’ve tried to override the php config in an htaccess file and with ini_set. Nothing works.

    Reply

  5. Which values are you overriding? Session timeout seems to relate to session.gc_maxlifetime and session.cookie_lifetime.

    You can try creating a php file containing to test your .htaccess, as it will list all the php configuration values along with other useful information. Good luck :)

    Reply

  6. http://us.php.net/session

    Reply

  7. I got something to work. 3 ini_set configs at the top of every page. I couldn’t even put them in an include file!

    ini_set(‘session.gc_maxlifetime’, 60);
    ini_set(‘session.gc_probability’, 1);
    ini_set(‘session.gc_divisor’, 1);

    The above values would cause the session to expire in 1 minute.

    Reply

  8. Scratch that – they do work in an include file – as long as the include is above the session_start()

    Reply

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress