I have 3 pages a.php ,b.php and c.php .How can I use the variable of b.php in c.php


a.php

<script type="text/javascript">
var width;
width = screen.width;
if (width > 0) {
location.href = "b.php?width=" + width;
} else 
    exit();
</script>

b.php

<?php

if($_GET['width'])
{
public $screen_width;
$screen_width=$_GET['width'];
}

else
{
include("a.php");
}
?>

c.php

<?php 
global $screen_width;

 if($screen_width>1024)
{
.....
}

else

{...

}

?>

Help me guys,I can't use include() since a.php has js that keeps reloading everytime and c.php will be redirected to c.php?width=...

Dani AI

Generated

The root problem is that PHP variables do not persist across separate requests. has a client-side detector (a.php) that sends the width, and b.php/c.php are separate requests — so sharing a plain PHP variable between them will not work without a persistence mechanism (query string, cookie, session, server storage). 's session idea is valid, but the blank page symptom is a strong sign of a PHP error in b.php (the snippet shown uses the public keyword outside a class, which causes a parse error) and/ or suppressed errors. Including a.php will re-run the JavaScript detector and create a redirect loop; avoid including pages that emit client-side redirects.

Quick diagnostics to run in development: enable full error reporting at the top of the PHP files and check the webserver/PHP error log. This will reveal parse/fatal errors and notices that explain the blank page.

Example alternatives (pick one):

  • Have the detector set a cookie from JavaScript and then navigate to c.php; c.php reads and validates the cookie.
  • Let the detector send the width to b.php, where the server stores it in session or a short-lived server-side store, then redirect to c.php which reads that stored value.
  • Send the width directly to c.php from the detector (query string) and validate it there.

Minimal examples (development helpers):

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);
?>

JavaScript cookie approach:

<script>
document.cookie = "screen_width=" + screen.width + "; path=/; max-age=3600";
window.location = "c.php";
</script>

Then in PHP read and validate the cookie before using it. Also ensure any session usage calls session_start() before output, and always validate/escape numeric input. For reference on PHP sessions and cookies see the PHP manual: session_start and setcookie.

Recommended Answers

All 2 Replies

use sessions:for example:

b.php

$var="hello";
session_start();
$_SESSION['var']=$var;

c.php

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

Enjoy!

Its not working, just blank page is being displayed when i open c.php .

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.