How can i pass a value from one webpage to another without using form,plz explain with example

Dani AI

Generated

Quick summary: there are several safe ways to pass data between pages without a form — short, public values can go in the URL; per-user private data should live on the server and the client can carry a short token; browser storage and cookies are alternatives for client-only state. Which to use depends on sensitivity, lifetime, and whether the server needs to read the value.

Always validate and escape input. For example, validate an expected integer and escape before output:

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if ($id === false) {
    // handle invalid input
    exit;
}
echo htmlspecialchars((string)$id, ENT_QUOTES, 'UTF-8');

If the data is sensitive do not put it in the URL. As showed a client redirect is possible, but avoid sending passwords or secrets that way — URLs are stored in browser history, server logs and referer headers. For per-user sensitive data prefer server-side sessions or a server-side token (store data in DB/session and pass only a short lookup token in the URL).

When using sessions, apply best practices: regenerate the session id after privilege changes, set secure cookie attributes, and enable strict mode. Example for setting a secure cookie (PHP 7.3+):

// PHP 7.3+ to set secure, HttpOnly and SameSite
setcookie('token', $token, [
  'expires' => time()+3600,
  'path' => '/',
  'secure' => true,
  'httponly' => true,
  'samesite' => 'Lax'
]);

For purely client-side state use sessionStorage/localStorage or the URL fragment (hash). Fragments are not sent to the server and are parsed by JS:

localStorage.setItem('userId', '12345');
const id = localStorage.getItem('userId');

const params = new URLSearchParams(location.hash.slice(1));
const value = params.get('data');

Tiebacks: covered passing values via GET — if you use that pattern validate and keep values short; suggested sessions — use them for sensitive info and harden via cookie flags and session_regenerate_id. Practical checks: test with devtools, verify cookies are sent, and avoid URLs larger than a couple thousand characters.

Recommended Answers

All 3 Replies

window.location='url.php?user=blah&password=blah'

You can also try this one.

for example on page1.php you can have a link that when clicked will pass variables to page2,php

page1.php

<?php
echo "<a href='page2.php?var1=1&var2=2&var3=3>"."link </a>";
?>

page2.php

on this page you will retrieve the values using $_GET

<?php 

$var1=$_GET["var1"];
$var2=$_GET["var2"];
$var3=$_GET["var3"];

echo "$var1";
echo "$var2";
echo "$var3";

?>

you could use sessions to store variables between pages.For example:

page1

session_start();
$_SESSION['var1']="this is variable1";

page2

session_start();
echo $_SESSION['var1'];

output of page2 will be: this is variable1

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.