0

my html code

<form action = "search.php" method="get">
    <input type="text" name="destination">
    <input type="submit" name="submit" value="submit">
 </form>

my php code

<?php
  if(isset($_GET['submit'])){
    echo $_GET['destination'];
 }
?>

MY URL

http://localhost/vb/guest/search.php?destination=USA

now i want that if i change the value of the variable then it will remain same that means if destination=canada or any other string then it will set the value USA again. please help, i m new to PHP

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You could store it in the `$_SESSION` or a cookie and then if it's different [redirect to the right page](https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – JCOC611 Nov 08 '17 at 23:37
  • Your question is very unclear. *"Change the value of the variable*" where and what variable? – Phil Nov 08 '17 at 23:37
  • I also don't underrstand. What's the point of having the parameter if it will always be set to `USA` again? – Barmar Nov 08 '17 at 23:39
  • i thought so .. is there any way without session to do that –  Nov 08 '17 at 23:39
  • Anything other than a session would probably allow the user to override what you're doing. They can change cookies, for instance. – Barmar Nov 08 '17 at 23:41

1 Answers1

0

Try something like this:

$pathToUSA = '/search.php?destination=USA';

if (@$_GET['destination'] !== 'USA') {
    header('Location: ' . $pathToUSA);
}

hope it helps.

Mark
  • 780
  • 1
  • 6
  • 17
  • yap.. it good.... but my target is to keep all get variable will remain unchanged do you have any idea about this ????? –  Nov 09 '17 at 00:53
  • If you are using a framework they maybe having a flash session (session is set and destroyed automatically once used). Other than using "frameworks" flash session you may implement it your self (set session and destroy it after using it). Storing your destination in session I think would help but I'm sorry I'm not sure if it is the best practice for it. – Mark Nov 09 '17 at 01:04