0

I have a form that when submitted appends the current page's URL: domain.com becomes domain.com?name=value

The form works great, but I need to write a cookie so that when the user goes to another page, the url will retain the string that has been appended to the URL.

domain.com?name=value
domain.com/page?name=value
domain.com/page2?name=value

etc.

ultranaut
  • 2,132
  • 1
  • 17
  • 22

1 Answers1

0

If passing variables between pages of PHP is what you want, then pass it through a session variable:

In all pages of your project, add the following line at the top:

<?php
    session_set_cookie_params(0);
    session_start();
?>

Then assign a value to a session variable in one page:

$_SESSION['name'] = value;

Retrieve this variable in another page that is within the same session:

$name = $_SESSION['name'];

At any point when you are done with keeping the session open, call

<?php 
    session_start();
    $_SESSION = array();
    session_destroy();
?>

Take a look at this SO post for a method to completely destroy a session. Remember that session by default stays open until the browser is closed or session is explicitly destroyed.

If dealing with sessions is not what you want, then you can manually pass the variable whenever you are calling a script within your PHP:

<?php 
    $name = $_GET['name'];
    echo "<form action='nextpage.php?name=$name' method='post'> ... </form>";
?>

This way all of your forms send the name variable through GET to the next page.

Community
  • 1
  • 1
Bee
  • 2,472
  • 20
  • 26