How do i add variables from

$var1=urlencode(''.$var1.''); 

on page one.php
and use the values from the above variables on page two.php

$var1 = $criteria['".$var1."'];
if($var1=='') $var1 = '';

Page two is included in page one and I am trying to use the same variable value.

Dani AI

Generated

Quick summary and recommended approach. If page2.php is literally included into page1.php (so page1 runs and then pulls in page2), variables defined in page1 before the include are available in the included file; order matters. For persistence across separate requests (clicks / redirects) use a session. Cookies work too for longer-term client-side storage but are not a replacement for sessions when you need server-side, per-visitor state. Thanks to for the session suggestion, and for the persistence ideas, and for the include-scope point — the advice can be combined depending on whether you need same-request access or cross-request persistence.

Example (sessions — put this at the very top of every script that reads/writes the value):

<?php
session_start();                // must run before any output
$_SESSION['var1'] = $var1;      // store on one page
// on another page:
session_start();
$var1 = $_SESSION['var1'] ?? ''; // retrieve with a safe default

Practical tips and gotchas:

  • If you rely on include/require, make sure the variable is set before the include statement; otherwise it will be undefined inside the included file. Variables declared inside a function are not available to the global scope unless exported with global.
  • Always validate and sanitize values coming from users (use filter_input/filter_var and htmlspecialchars when echoing) to avoid XSS or injection issues.
  • Call session_start() before any output; if sessions don't persist, check browser cookie settings and server session path/permissions.
  • Avoid double-encoding values (encode only when inserting into a URL), and avoid stuffing large binary data into sessions.

Checklist: decide whether you need same-request access (define before include), per-visitor persistence (use sessions), or long-term client storage (cookies); initialize early; validate inputs; and inspect $_SESSION/$_COOKIE with var_dump() while debugging.

Recommended Answers

All 5 Replies

You want to cash those variable with visit scope (if I got your question right) …so why don’t you use session ?

As jkon said, just use a cookie or a session. So lets say this is page1.php:

<?php 

    $var1 = 55;
    setcookie('var1', $var1, time() * 60*60*24*7); //this cookie will expire in one week

?>

then in page2.php, if you want to access $var1 then:

<?php 

    $varFromAnotherPage = $_COOKIE['var1']; //now we will retrive the cookie and its value by using $_COOKIE[];

?>
Member Avatar for Member #972282

Two Options
1: Pass it from one page to another using sessions or post data
2: Use an include or require.

PHP Include and Require Tutorial

Using php variables

How do i add variables from

on page one.php
and use the values from the above variables on page two.php

$var1 = $criteria['".$var1."'];
if($var1=='') $var1 = '';
Page two is included in page one and I am trying to use the same variable value.

Quoted Text Here

Don't use cookies are a slow thing.. you can do better this:
page1.php

$var1=urlencode($var1); 
$var2=urlencode($var2);

page2.php

include('page1.php');
if($var1==''){$var1 = '';}
if($var2==''){$var2 = '';}

If page two is included in page one you can use variables from page one in page two straight away. If they are different variables they should have different names.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.