Passing variables between scripts

Hi

I have two scripts and want to transfer one variable from one script to another but cannot get it to work.

Script one has a variable called $book_mobi

Script two needs to display and use that variable.

I have tried global and session without success.

Any help would be greatly appreciated please

Thanks

Show us what you tried with Sessions.

session_start();

$message1 = “A message”;

$_SESSION[‘firstMessage’] = $book_mobi;

Where does $book_mobi value come from? Is this the first page or second page?

Using a session variable is quite simple.

page1.php

<?php
session_start();
$_SESSION['mySessionVariable'] = 'My Session Data';

page2.php

<?php
session_start();
echo $_SESSION['mySessionVariable'];

Ok set Page 1 with:

session_start();

$_SESSION[‘mySessionVariable’] = $book_mobi;

and Page 2 with:

session_start();

echo $_SESSION[‘mySessionVariable’];

but the variable is still not showing

You didnt answer my question. Where does the value for $book_mobi come from? Are you sure it contains data? What do your error logs say?

Did you try the code as I posted it?

The variable value for $book_mobi comes from page one - it points to an https file on the server.

I tried the code as you sent me in both pages but when I view the source code of page two it does not show the session code

Page Two code is:

        <legend>Please fill the following form to contact us TEST</legend>
						<?php

session_start();
echo $_SESSION[‘mySessionVariable’];
?>

But viewing the page source code I get:

        <legend>Please fill the following form to contact us TEST</legend>
						

        <form  method="post" action="">

I’m getting an error log:

[24-Jun-2022 03:40:48 America/New_York] PHP Notice: Undefined variable: phone in /home/bookxiev/public_html/library_2031/cform.php on line 154

That still doesn’t answer the question asked. If $book_mobi is empty, or contains a false value, all the code being used to store it in a session variable, then echo it later could be working, but echoing it won’t show anything. Instead of echoing the session variable, for debugging purposes, use var_dump($_SESSION[‘mySessionVariable’]);

The Notice: Undefined variable: phone… message isn’t coming from any session variable code. That’s for a regular variable, i.e. $phone. Also, you are using an older version of php. In php8, that would be a Warning message.

You are showing code with the session_start() located after content on the page. This will normally not work and produce a Warning message. This can ‘work’ if php’s output_buffering is turned on. However, an issue with output_buffering being on is it hides non-fatal php errors and other output from your code if you are doing redirects. So, three things -

  1. Is php’s error_reporting set to E_ALL, so that ALL php errors will get reported?
  2. Is php’s output_buffering turned on?
  3. How are you getting from the first page, where the session variable is being set, to the second page, where you are trying to use it? A redirect? A link?

For finding out the first 2 items, you can use a phpinfo() statement in a .php script file, then search for those settings in the output.

I’m also wondering why are you are needing to do this for this value? Generally, you would only store things in session variables that you need to persist between page requests, such as the id of a logged in user, a language choice, …

Lastly, I recommend that you start with two new files, only put the session setting/use code in them, and get this to work first.

Sponsor our Newsletter | Privacy Policy | Terms of Service