Friday, July 4, 2008

When users submit incomplete links, quite unknowingly so

I myself didn't notice this earlier...
If one creates a link to a page 'x.php' (e.g. <a href="x.php">), then the resolved address points to that page on the same website. To link to another website, you provide the full address, (e.g. <a href="http://www.saos.org/x.php">).
But if you provide www.saos.org or saos.org (no www) for the href attribute, the browsers don't transform it into an external link, quite logically so. It will point to yoursite.com/www.saos.org

This was something I came across while fixing up commenting on {ego}trips. If one put a sitename without http:// it would show up as a link local to our website, which is NOT welcome. There is nobody to blame, though.
Fixing this was simple, though. I just appended a http:// to those which were missing it, using PHP's parse_url() function.
Note: haven't checked, or tried to append www to the links without it.

//here is a sample list of links of varying types.
$urls = array('www.google.com', 'http://www.yahoo.com', 'microsoft.com', 'www.saos.org/x.php');

//here is where we transform, then output them
foreach($urls as $url) {
$parsed = parse_url($url);
echo '<a href="';
if(empty($parsed['scheme']))
echo 'http://';
echo $url;
echo '">'.$url.'</a><br />';
} //end of foreach loop
?>

Wednesday, July 2, 2008

exg anew

Started with exg - the 'thing' that drives {ego}trips on my website - once again, after almost an year. I was skeptical about getting a hold of the code again, but that wasn't much of a problem. I can easily understand my retard coding practices anytime.

I wished to make some changes, which I started to pen down (and simultaneously work upon a solution).
Change #1 was to put a 'latest uploaded galleries' listing on the homepage. Quite simple, isn't it? But again, generating relative rankings was a bit of a problem. There didn't seem any way that SQL would work. Ideal way to tackle it would've been through complex queries, but I chose to put PHP to use to do so.

That led me to discover some serious flaws in my earlier scripts, which notably had to do with relative ranking of image galleries (with respect to trips) and the consequent changes on enabling/disabling them. Not only did it lead to some errors but also exposed private galleries.

Then I came to realise that there was some problem in the clan page as well. Private galleries were also visible through the generated links. Fixed that as well.

By the end, my sheet was littered with headings:
Task
-Problem
--Problem
---Bigger Problem
----Added Problem
-Another Problem
--Existing Problem
-Still a Problem

Ended up jumping between code for about 3 hours. Quite a sudden change in my daily routine of idleness. Here's a small query to pull some stuff from the database. BEAT THAT!

select * from
(select @rn := @rn + 1 as rank, ptjoin.*
FROM (SELECT pm.perspective_id, pm.trip_id, t.trip_title, pm.title,pm.member_id,pm.member_name
FROM (
SELECT p.perspective_id,p.trip_id,p.title,p.member_id,p.created,m.member_name
FROM perspective p
INNER JOIN members m
ON p.member_id=m.member_id
WHERE p.trip_id = XXXX
)as pm
INNER JOIN trip t
ON pm.trip_id=t.trip_id
ORDER BY pm.created
) as ptjoin
) as rankedptjoin
where rankedptjoin.rank = XXXX;


Now I'll start with building the feature to upload docs for the trips, anything that helps.