Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

301 redirect with PHP

For redirects on servers that have .htaccess locked down:

header('HTTP/1.0 301 Moved Permanently');
header('Location: http://new-page.com/');
header('Connection: close');
exit();

PHP redirect code (with correct header)

<?php
header ('HTTP/1.1 301 Moved Permanently');
header('Location: http://domain.com/');
exit;
?> 

dynamic rails img headers

// description of your code here

find views -name [a-z]\*rhtml | xargs -n1 grep -H "@page_title" | grep -v "<%" | sed "s/\@page_title = //" | sed "s/rhtml/png/" | sed "s:views/::" | sed "s:/:_:g" | sed "s:^:public/images/beta/headers/hdr_:" | sed "s/  //g"

Resolving a tinyURL to destination URL

Resolving a tinyURL to its original destination URL in a fastest way.

    < ?php

    // request like this http://<domain>/tinyurl.php?c=<tinyurl>

    $num = $_GET['c'];

    if($fp = fsockopen ("tinyurl.com", 80, $errno, $errstr, 30))
    {
    if ($fp) {
    fputs ($fp, "HEAD /$num HTTP/1.0\r\nHost: tinyurl.com\r\n\r\n");
    while (!feof($fp)) {$headers .= fgets ($fp,128);}
    fclose ($fp);
    }
    $arr1=explode("Location:",$headers);
    $arr=explode("\n",trim($arr1[1]));
    echo trim($arr[0]);
    }
    ?>


http://www.webforth.com/2007/07/resolving-tinyurls-to-the-desination-url

How to draw your own table header... (with borders)

// It's a bit hacky at the beginning, but nstableheadercell wasn't exactly making things easy for me

- (void)_drawThemeContents:(NSRect)cellFrame highlighted:(BOOL)highlighted inView:(NSTableHeaderView *)view;
{
	int index = [view columnAtPoint:NSMakePoint(cellFrame.origin.x + 1,cellFrame.origin.y + 1)];
	NSRect headerRect;
	if(index != -1)
		headerRect = [view headerRectOfColumn:index];
	else
		headerRect = NSZeroRect;
	
	[headerImage drawInRect:cellFrame
				   fromRect:NSZeroRect
				  operation:NSCompositeSourceOver
				   fraction:1.0];
	
	[[NSColor colorWithCalibratedRed:207.0/255.0
							   green:207.0/255.0
								blue:207.0/255.0
							   alpha:1.0] set];
	NSRectFill(NSMakeRect(headerRect.origin.x, headerRect.origin.y + 1, headerRect.size.width, headerRect.size.height - 2));
	[headerImage drawInRect:NSMakeRect(headerRect.origin.x, headerRect.origin.y, headerRect.size.width - 1, headerRect.size.height)
				   fromRect:NSZeroRect
				  operation:NSCompositeSourceOver
				   fraction:1.0];
}

Turn CSV with headers into Array of Hashes (in 5 lines or less)

This assumes you have a CSV file whose first line are headings/labels for the individual columns.

require 'csv'

csv_data = CSV.read 'data.csv'
headers = csv_data.shift.map {|i| i.to_s }
string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }
array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] }

Cannot modify header information - headers already sent by FIX

Buffers output and eliminates problem with spaces/line breaks at the beginning/end of files. Make sure to place ob_start(); before header(); is called.
<?php
ob_start();

header("Location: somepage.php"); //Redirect

ob_end_flush();
exit;
?>
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS