Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.
Note: HTTP/1.1 requires an absolute URI as argument to Location:
including the scheme, hostname and absolute path, but some clients
accept relative URIs. You can usually use $_SERVERї'HTTP_HOST'],
$_SERVERї'PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself:
<?php
header("e;Location: http://"e; . $_SERVERї'HTTP_HOST']
. dirname($_SERVERї'PHP_SELF'])
. "e;/"e; . $relative_url);
?>
I'm now using the following function for redirecting pages. Any comments appreciated.
As I posted in the original thread, you'll want to check if a session is active and if the cookie ISN'T set, if both are true you'll want to append the SID here as PHP won't append it for you inside header calls.
@nielsene: Meaby i don't get it... But & is only used in html
@pilau: it works like this:
- it adds http or https
- then it looks at the portnumber and if it's not the default 80/443 it adds the portnumber
- then we add the servername
- if the location doesn't start with / we calculate the base path and add that
- then we add the location
- then we add an eventual session id
In XHTML the '&' is not allowed to appear in the source html, except as part of an entity reference. This means you can't have
"plain" '&'s in your query strings if you want the page to validate according the various XHTML DTD's. You can however you use the entity reference & ; (remove the space between amp and ';' in actual use) in your urls and they still work right while maintaining validability.
timvw wrote:
In case you want to do more kinky stuff, one could also consider using urlencode.
Actually as that link says, urlencode doesnot handle the &->& ; conversion, you have to either use htmlspecialchars/htmlentities, or change the arg_seperator in php.ini, or deal with it manually. And in many cases you still have to deal with it manually if you're dynamically generating urls.