Get content after question mark in PHP if statement
I am setting up a separate theme page for printable content. To tell the b开发者_如何学Gorowser to use the print theme I'm using a mark ?print after the URL. So how does one get that content?
Something link
<?php if (? = 'print') {
blah blah }
Any ideas?
Marvellous
You can access the whole query string using $_SERVER['QUERY_STRING']
.
So your code could look like this:
if($_SERVER['QUERY_STRING'] == 'print') { /* do something */ }
You use $_GET
array to access that.
If you want the whole string, use $_SERVER['QUERY_STRING']
.
In your example, you'd do something like this...
if (isset($_GET['print'])) {
// something.php?print
};
You need to use $_GET
:
$_GET['print']
An associative array of variables passed to the current script via the URL parameters.
Note: Beware of security though, you need to sanitalize and validate any data coming from the URLs.
Are you aware you can just set up a stylesheet for printing, this may suffice for your needs. Just add a line like the following after your standard css line.
<link rel="stylesheet" href="print.css" type="text/css" media="print" />
精彩评论