How do I get the current URL of the page I am currently on? Also how would I get the URL of the page which referred me to the page I am in now?
$currentPage = htmlspecialchars($_SERVER[’PHP_SELF’]); $referrerPage = htmlspecialchars($_SERVER[’HTTP_REFERER’]); You need to parse them for HTML or else they're potentially vulnerable to XSS vectors (Eg. http://www.example.com/register.php?"<script>alert('XSS')</script> if you're using PHP_SELF as a form action)
scriptman, he wants the URL, not the script path (and possibly query string) - note that they are not always the same, e.g. when rewriting URLs. $currentPage = $_SERVER['REQUEST_URI'];
Thanks for pointing out my misinterpretation, krt. I'd just gotten used to using PHP_SELF for relative hyperlinks
SO let me get this straight: //current URL $currentPage = $_SERVER['REQUEST_URL']; //referer URL. what does htmlspecialchars() do? $referrerPage = htmlspecialchars($_SERVER[’HTTP_REFERER’]); Code (markup):
For further reference on what functions do, go to www.php.net and type the function name in the search field. If the information there is unclear or you still don't understand.... then ask. And according to these weird quotes, this code has probably been copied from a blog or something, but there should be normal single or double quotes.
From the php manual page on $_SERVER: <?php $self_url = sprintf('http%s://%s%s', (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == TRUE ? 's': ''), $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ); ?> PHP:
Actually I wrote it out straight into the DP message form. I hadn't noticed the strangeness of those quotes but I used the standard key... Testing: ' ' ['test'] $referrerPage = htmlspecialchars($_SERVER['HTTP_REFERER']); ...That's very weird. enchance, Just noticed your question that's hidden inside a quote box. Htmlspecialchars escapes HTML characters. This helps prevent XSS attacks if you use HTTP_REFERER at the HTML level (for example someone could craft a link that grabs document.cookie and redirects you to their own site, where they record your cookie information).