Use SESSIONS variables between different php files

I am new to $_SESSIONS , but it needs to reuse variables between different php files. In the code below, I want to use the $word variable in another php file, but I'm not sure how to do this.

My php file is as follows:

 <?php if (isset($_POST["search"])) { //include database connection $word = mysql_real_escape_string($_POST["search"]); $word = htmlentities($word); $sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10"); $results = mysql_query($sql); if (mysql_num_rows($results)==0) { echo $word, " text bla"; }else { echo $word, " text bla bla"; while ($row = mysql_fetch_assoc($results)) { echo '<pre>', print_r($row), '<pre>'; } } }?> 

Wait for your offers.


--- UPDATE Sessions still not working on page 2.php? ---

I do not understand why $_SESSION does not work. One page1.php I can echo($_SESSION['word']) and get the correct value, but one page2.php I get ('$'."_SESSION['word'] isn't set because you had never been at file one");

I tested all the solutions below, but none of them worked = the same result on page 2.php.

My file is page1.php.

 <?php session_start(); //if we got something through $_POST if (isset($_POST["search"])) { // include database connection $connect = mysql_connect('localhost', 'root', 'NomiS123') or die(mysql_error()); mysql_select_db('workcard'); // sanitize user input $word = mysql_real_escape_string($_POST["search"]); $word = htmlentities($word); // build search query to the database $sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10"); // get results $_SESSION['word'] = $word; $results = mysql_query($sql); if (mysql_num_rows($results)==0) { $_SESSION['word'] = $word; echo($_SESSION['word']. "<br>"); var_dump($_SESSION); echo "<br>"; echo "link link <br>"; echo "<a href=\"../page2.php/\">new card</a> <br>"; echo "<a href=\"//cykelc/\">New Search</a>"; } else { echo $word, " bla bla text <br> Create card <br>"; echo "Edit info on: ", $word, "<br>"; echo "<a href=\"//cykelc/\">New Search</a> <br>"; while ($row = mysql_fetch_assoc($results)) { echo '<pre>', print_r($row), '<pre>'; } //$results->free(); } } // mysql_close($connect); ?> 

My file is PAGE2.php.

 <?php session_start(); if(isset($_SESSION['word'])) { $word = $_SESSION['word']; echo($word); } else { die('$'."_SESSION['word'] isn't set because you had never been at file one"); } ?> 

I'm going crazy about this.


UPDATE - SOLVED
I tested all of the recommendations below, but none of them worked, which was strange because I could set and respond sesson_id() on page1.php and page2.php, but on page22.php I have another sesson_id() . I began to study the settings of the MAMP sessions, but everything was right. The solution was β€œsimple” to put session_start(); to the very top on page 2.php. And at the very top, I mean up to everything even <!DOCTYPE html> , etc.
Solved + lesson learned :-)

+6
source share
6 answers

First you must start browsing through session_start(); immediately after opening the PHP tag '( <?php session_start();... ?> )

Then you must save the variable in the session. You can use $_SESSION['word'] = $word; for this purpose.

And in another file you should also use session_start(); at the very beginning after the <?php tag.

Then you can access the old variable via $word = $_SESSION['word']; .

Now you can also use $word in the second file. But you can use it only if it is installed (and you, where in the first file before).

File one:

 <?php session_start(); if (isset($_POST["search"])) { //include database connection $word = mysql_real_escape_string($_POST["search"]); $word = htmlentities($word); $_SESSION['word'] = $word; $sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10"); $results = mysql_query($sql); if (mysql_num_rows($results)==0) { echo $word, " text bla"; }else { echo $word, " text bla bla"; while ($row = mysql_fetch_assoc($results)) { echo '<pre>', print_r($row), '<pre>'; } } }?> 

File two:

 <?php session_start(); if(isset($_SESSION['word'])) { $word = $_SESSION['word']; } else { die('$'."_SESSION['word'] isn't set because you had never been at file one"); } echo $word; ?> 

Hope this helps;)

+2
source

To use PHP sessions, you must do the following:

Initiate a session, session_start();

Note. session_start(); should be the first line of PHP in your file.

Create a session, $_SESSION['word'] = $word;

To access it on another page:

Initiate a session, session_start();

Access to the session, $word = $_SESSION['word'];

+2
source

session_start() means you are using session variables, make sure they are at the top of the page. To complete a session, do the following: $_SESSION['word'] = [some value] . This can be used between pages as long as you have session_start() at the top. Be sure to make sure it is installed first if it is not installed.

 <?php session_start(); ?> ... <?php if ( isset($_SESSION['word']) ) { $_SESSION['word'] = /* change existing session value */; } else { $_SESSION['word'] = /* new session value */; } ?> 
0
source

First of all, you should start a session using the session_start () function. Put this in your index.php / bootstrap.php (the file that always loads when your site loads).

After that, you can use the global '$ _SESSION' to set the data.

 //In your index.php/boostrap.php session_start(); 

In the file you want to save $ word:

 $_SESSION['myword'] = $word; 

And from now on, you can use this variable on another page.

 //On another page, after the variable is being set echo $_SESSION['myword']; 

Remember that when you use shared web hosting, your session data is often stored in a global folder on the web server and can be used by every site on this server. To prevent this, you must either modify your session save path using the session_save_path () function or create your own session handler.

0
source

Call session_start(); at the beginning of the PHP file, and if something is set in the $_SESSION supermassive $_SESSION , you can re-access it.

If it is not installed, you can install it with:
$_SESSION['a name']= 'some thing';

So for your example:
PHP FILE1

 <?php session_start(); $_SESSION['word'] = $_POST['word'] ?> 

PHP FILE2

 <?php session_start(); echo $_SESSION['word']; ?> 
0
source

There are many ways to do this, it all depends on how you want to use it. You can include your file in this file, you can use require or require_once, or yes, you can use a super global session.

The $ _SESSION supercomputer will be available for all files in your application. The only thing you need to make sure that you do this is use session_start () on the page, as the first thing on this page. If you use session_start () after exiting any exit to the browser, this will not work. Usually you need to run session_start () as the first line in the index.php file. Then you can use ...

 <?php if (isset($_POST["search"])) { //include database connection $word = mysql_real_escape_string($_POST["search"]); $word = htmlentities($word); $_SESSION['word'] = $word; $sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10"); $results = mysql_query($sql); if (mysql_num_rows($results)==0) { echo $word, " text bla"; }else { echo $word, " text bla bla"; while ($row = mysql_fetch_assoc($results)) { echo '<pre>', print_r($row), '<pre>'; } } }?> 

then on any page that you want to access, just copy it ...

 <?php echo $_SESSION['word']; 

Hope that helps

0
source

Source: https://habr.com/ru/post/977567/


All Articles