Old session counter in PHP

I just wanted to share some old code I got lying around. It’s a session counter in PHP basically what it does is to increase a variable with one each time a page with the code is refreshed/visited. I even have a live example of it.

The code:

<?php
// Start sessions!
session_start();
// Is $_SESSION["count"] set? If yes, increase by one, if not set to one.
isset($_SESSION["count"]) ? $_SESSION["count"]++ :  $_SESSION["count"] = 1;
echo "This page has been loaded: {$_SESSION["count"]} time(s).";
?>
<br /> Reload the page. It's a simple counter using sessions to keep the count.

Each time you refresh the page the the count will increase with one. Remove the cookie to start from 1 again.

Posted in PHP, Programming | Leave a comment

Bypass Google Images intermediary page

Ever wanted the Google Image search results to go directly to your page?

Look no further! This goes into your page, and it will make make the users’ browser refresh once your page is loaded under the picture that google displays.

<script type="text/javascript">
if (top.location != location) {
  top.location.href = document.location.href;
}
<script>

I might make some pictures for show and tell later.

Posted in Design, Javascript, Programming | Leave a comment

XOR-Swap

I’m having a lot of fun writing a for-fun play-money online casino these days. Every day you learn something new, and about a week ago I learned about XOR-Swap. The idea is very simple, yet very effective. The website I’m working on is written in PHP, so I’ll present the code in that language as well, but it’ll work in any language that can do eXclusive OR.

<?php
a = 10;
$b = 44;
$a^=$b^=$a^=$b;
//$a = 44;
//$b = 10;
?>

If you’re having trouble reading that it might be a bit easier to write it all out.

<?php
$a = 10;
$b = 44;
$a = $a^$b;
$b = $b^$a;
$a = $a^$b;
//$a = 10;
//$b = 44;
?>

You might be thinking; “why not do just assign a temporary variable instead“, and that works great too. However you won’t feel warm and fuzzy inside. You won’t be able to show off with your one-line ninja code either. Assigning a temporary variable and XOR-Swap is about equal in performance – I’ve benchmarked this. A temporary variable will leave a footprint equal to the size of your variable, but memory is cheap, right? However using something like list($a, $b) = array($b, $a); is a lot slower – if you think about it that makes sense as you actually have to populate the array before assigning it to the variables in the list. When I say a lot slower, I mean a lot slower. I might post benchmarks for this in a later post.

One thing you have to worry about is if the the two variables you’re XOR-Swapping are not the same type or if they are of unequal length. XOR-Swapping $a = "hello"; $b = "world"; will work, but $a = "hello"; $b = "world!"; will break.

Posted in Optimization, PHP, Programming | Leave a comment

Posts coming to a blog near you!

It took me a year, but I promise I’ll start writing some posts on this blog in the very near future.

I don’t know if anyone will find what I write useful or even interesting, but if you do I would love to hear about it! Feel free to drop a me a personal message or write a comment. I’m looking for some work, so if you need somebody to do freelance for you hit me up!

Posted in Life | Leave a comment

First post!

Those who wait for something good

Might find themselves waiting forever…

Posted in Life | 1 Comment