check if a querystring exists, if not create one - PHP check if a querystring exists, if not create one - PHP php php

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


This should work:

if(isset($_GET['id'])){    //It exists}else{    //It does not, so redirect    header("Location: Page.php?id=1");}


Do something like:

if(!isset($_GET['id'])){  header('LOCATION:www.mysite.co.uk/Folder1/Folder2/Page.php?id=1'); die();}