PHP to check if a URL contains a query string
For any URL as a string:
if (parse_url($url, PHP_URL_QUERY))
http://php.net/parse_urlIf it's for the URL of the current request, simply:
if ($_GET)
Check if url contains parameters
The URL parameters are received from a global variable called$_GET
which is in fact an array. So, to know if a URL contains a parameter, you can use isset()
function.if (isset($_GET['yourparametername'])) {
//The parameter you need is present
}
Afterwards, you can create separate array of such parameter you need to attach to a URL. Like:if(isset($_GET['param1'])) {
\\The parameter you need is present
$attachList['param1'] = $_GET['param1'];
}
if(isset($_GET['param2'])) {
$attachList['param2'] = $_GET['param2];
}
Now, to know whether or not, you need a ?
symbol, just count this arrayif(count($attachList)) {
$link .= "?";
// and so on
}
Update:To know if any parameter is set, just count the $_GET
if(count($_GET)) {
//some parameters are set
}
check if a querystring exists, if not create one - PHP
Maybe there are plenty of ways. You can assign value to $_GET key if one does not exist. Or if you really need to query string, you can renavigate the user to the same page with present querystring.
if (!isset($_GET['id'])) {
header("Location: Page.php?id=1");
exit;
}
It should be before any output in the page. So if user visits Page.php
or Page.php?
or Page.php?someDifferentParamThanId=10
it will return false on isset($_GET['id'])
thus it will redirect to Page.php?id=1
How to check if url has no query string in jQuery?
You can use indexOf
on the window.location.href
to check if there is a ?
character.
Note that you can name the jQuery instance provided to the document.ready handler as you need, so you can still use the $
variable within that function scope, even through the global $
no longer points to jQuery. Also, you can make your jQuery code more efficient by caching and re-using your selectors. Try this:
jQuery(function($) {
if (window.location.href.indexOf('?') == -1) { // no querystring exists
$('#login #user_login').attr('placeholder', 'Username').addClass('form-control');
$('#login #user_pass').attr('placeholder', 'Password').addClass('form-control');
$('#login #loginform label').wrap("<div class='input-group'></div>");
$('#login .form-control').each(function() {
$(this).insertAfter($(this).parent());
});
$('#loginform .input-group > label').remove();
$('<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></span>').insertBefore('#user_login');
$('<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></span>').insertBefore('#user_pass');
}
});
Check in PHP if a specific query is in URL
Use array_key_exists to check queryA
key present in $query
array
$parts = parse_url('http://my url?queryA=valueA&queryB=valueB&queryC=valueC');
parse_str($parts['query'], $query);
if (array_key_exists('queryA', $query)) {
echo "queryA element is in the array";
}
How to check the query string of my current url for a specific key-value pair?
You can access the Query parameters using the $_GET superglobal, In this case:
if(isset($_GET['special_offer']) && $_GET['special_offer'] == 12){
echo "Something";
}
Related Topics
How to Select Year and Month from the Created_At Attributes of Database Table in Laravel 5.1
Randomize a PHP Array with a Seed
Resource Interpreted as Image But Transferred with Mime Type Text/HTML - Magento
Number in the Top-Level Domain
Doctrine2 - "Class" Is Not a Valid Entity or Mapped Super Class
Creating a File Progress Bar in PHP
PHP Pdo Bindparam Was Falling in a Foreach
Dynamic Class Property $$Value in PHP
Why Should I Never Run 'Composer Update' in Production
Send Zip File to Browser/Force Direct Download
How to Convert Latin1_Swedish_Ci Data into Utf8_General_Ci
Laravel Catch Eloquent "Unique" Field Error
Type Hinting: Default Parameters
Symfony2 & Doctrine: Create Custom SQL-Query
Find Array Key in Objects Array Given an Attribute Value