how to access the variable from another php file - php

I have an array in one php1 file which I want to access in another php2 file but if i use either include or require once am also getting the output of the php1 file. I want to access only the variables from php1 but not the output. Can anyone suggest how to do that. In php1 I have lot of menus and boxes which I don't want to include in php2 except the variables.
<?
session_start();
require_once('home.php');
if($_GET["reset"]==1)
{
session_destroy();
header('Location: index.php');
}
?>
if i use this am getting all the output from the home.php page. I want only the variables.

Convert the variables on the home page to be accessible via a function or better still, make them session variables

is there some way without using function or session to access the variables
As per your request. A simple skeleton to show you how it is done.
We pass the variable $myVar through the URL and use $_GET in the next page to use it. We use htmlspecialchars(); to clean user input.
We use the if (isset($_GET['myVar'])) so it doesn't throw Notice: Undefined index error.
pageOne.php
<?php
$myVar = "My variable one.";
echo "<a href='pageTwo.php?myVar=" . $myVar . "'>Page Two</a>"
?>
pageTwo.php
<?php
if (isset($_GET['myVar'])) {
$myVar = htmlspecialchars($_GET['myVar'], ENT_QUOTES);
// Now you can use $myVar.
}
?>

Related

POST variables not persisting from page to page

I'm trying to pass variables between consecutive pages of an automated php process. Because this is automated, I tried setting the variables like this in the prior page:
$_POST['index1'] = $variable1;
$_POST['index2'] = $variable2;
$_POST['index3'] = $variable3
$_POST['index4'] = "hard coded string";
I also have alert statements printing out these values on the prior page just to make sure they're getting set, which they are. But when I move to the following page and try to access those variables, I get undefined index errors for all of these variables. What's happening to the variables from one page to the next that they're not getting passed as expected?
Let's try the following since the information is sensitive.
At the very, very, very, very top of each of the PHP pages connected to this function add:
<?
session_start();
?>
Make sure nothing else is above it, no html, no nothing! Otherwise it will mess up whatever you are trying to do.
On the page you were originally setting your $_POST variables do the following:
$_SESSION["variable1"] = "You";
$_SESSION["variable2"] = "Shall";
$_SESSION["variable3"] = "Not";
$_SESSION["variable4"] = "Pass";
You can ofcourse change the "variable1","variable2" and all of its values into whatever you like.
On whichever page you have included
<?
session_start();
?>
You can now call echo/print/assign each of the $_SESSION variables you set before by echoing:
echo $_SESSION['variable1'];
This would produce the following: "You"
This is a more secure way of transferring data (however sessions can still be hijacked unless you're using HTTPS).
Whenever you are done using the data you can simply unset each $_SESSION variable by using either session_unset(); or session_destroy();
I hope this helps!

PHP How to send data to another page with header(redirect)

In my website the index will redirect to a controller that will access the DAO and needs set the data in a variable to show in the view. How do i set this datas? Is the $_SESSION the best way to do that?
I try the $_REQUEST, making this:
(Index.php)
<?php
$_REQUEST['test'] = "TEST!!!!";
$redirect = "controllers/controllerIndex.php";
header("location:$redirect");
(controllerIndex.php)
<?php
echo $_REQUEST['test'];
But i received the error:
Notice: Undefined index: test in C:\xampp\htdocs\PlataformaPHP\controllers\controllerIndex.php on line 2
As mentioned already, PHP SESSION variables are the solution you're looking for. It is important that you include session_start(); at the top of each PHP page that requires access to the session variable. Take a look at this which should help with your problem:
Index.php
<?php
session_start();
$_SESSION['test'] = "TEST!!!!";
$redirect = "controllers/controllerIndex.php";
header("location:$redirect");
exit();
?>
Controller.php
<?php
session_start();
echo $_SESSION['test'];
...
?>
You answered your own question, $_SESSION would be better.
You can also unset all your $_SESSION variables as described here
http://php.net/manual/en/function.session-unset.php
Using $_SESSION is probably the best solution, but the other option would be to just add the parameters to your header() url and they will be passed in the $_GET array.
$redirect = "controllers/controllerIndex/test";
header("location:$redirect");
Or without the Framework idea
header("Location: xxx.php?test");

use a php variable in diffrent pages

I have two pages of php on the same folder , Im wondering what can I do to use that variable in another page like page2.php
page1.php
$variable = "Variable";
page2.php
//get $variable value from page1.php here
Any suggestions? Thank You
Use Sessions.
On page1.php:
session_start();
$_SESSION['var1'] = 'some text';
On Page2.php:
if(!isset($_SESSION)){
session_start();
}
echo $_SESSION['var1'];
You will get some test as output.
Use session variables.
Just write session_start() at the beginning of files to start a session and assign the session variables.
eg:
File-1
session_start();
$_SESSION['var'] = "var";
File-2
session_start();
if(isset($_SESSION['var']))
echo $_SESSION['var']; // will print "var"
You can use this throughout till the session destroyed. To destroy the session (may be on logout)-
if(isset($_SESSION['var']))
unset($_SESSION['var']);
To completely destroy the session-
session_destroy();
Or you can try constants like so:
constants.php
<?php
define("VARIABLE_1", "Hotdog");
?>
page2.php
<?php
include_once("constants.php");
echo VARIABLE_1;
// output == Hotdog
?>
Just include the constants php script in the pages you want to use the variables in.
Check PHP Sessions:
page1.php
session_start();
$_SESSION['variable'] = "Variable";
page2.php
session_start();
echo $_SESSION['variable']
You can require_once("page2.php") in page1.php
That will allow you to use variables in page 2 across page 1.
Alternatively, you can also use sessions.
session_start() starts a new session or resumes an existing session. Include this on the first line of both pages.
Session parameters are set using $_SESSION["parameter-name"] = "something";
You can use the variables anywhere in the script.
To terminate sessions, use session_destroy()
You can use Sessions to achieve this , also you should know about QueryStrings.
For example:
Navigate to another page say "location2.php?Variable=123" here ?Variable is your var and 123 is value.
And on location2.php you can get this by simply $_GET["Variable"];
This can be achieved in many ways. You can use php session, you can include a file where you create that variable, or you can define it as a consant.
All of that depends how complex is that problem and which suits you best in this case.

Pass a variable value from one php page to another

I'm trying to send the value of this php get_field variable from a wordpress Archive page.
get_field('supplier_email_address', 'product_brand_' . $term->term_id );
to another sendmessage.php page which is never accessed other than with the of JS to send a form. The variable I need filled is called:
$sendto="value";
How can I achieve this?
EDIT: Apparently this requires more information.
Use session variables.
First of all every page you want to use sessions on needs to have the function session_start(); declared at the top of every page. You then can use the session superglobal to store things.
Pg1.php:
<?php
session_start();
$_SESSION['variable_name'] = 'string';
?>
Pg2.php:
<?php
session_start();
echo $_SESSION['variable_name'];
?>
Pg2.php will show: some string
Either POST your value so your second page can retrieve it in the $_POST array, or include it in the URL path so your second page can $_GET it.
Without more info, we can't really help you any more than that.

How to pass variables between php scripts?

Is there any way to pass values and variables between php scripts?
Formally, I tried to code a login page and when user enter wrong input first another script will check the input and if it is wrong, site returns to the last script page and show a warning like "It is wrong input". For this aim, I need to pass values from scripts I guess.
Regards... :P
To pass info via GET:
header('Location: otherScript.php?var1=val1&var2=val2');
Session:
// first script
session_start();
$_SESSION['varName'] = 'varVal';
header('Location: second_script.php'); // go to other
// second script
session_start();
$myVar = $_SESSION['varName'];
Post: Take a look at this.
You should look into session variables. This involves storing data on the server linked to a particular reference number (the "session id") which is then sent by the browser on each request (generally as a cookie). The server can see that the same user is accessing the page, and it sets the $_SESSION superglobal to reflect this.
For instance:
a.php
session_start(); // must be called before data is sent
$_SESSION['error_msg'] = 'Invalid input';
// redirect to b.php
b.php
<?php
session_start();
echo $_SESSION['error_msg']; // outputs "Invalid input"
Can't you include (or include_once or require) the other script?
The quick way would be to use either global or session variables.
global $variable = 'something';
The 'better' way of doing it would be to include the script and pass the variable by parameter like
// script1.php contains function 'add3'
function add3( $value ) {
return $value + 3;
}
// script2.php
include "script1.php";
echo 'Value is '.add3(2); // Value is 5
I would say that you could also store a variable in cache if you really need.
You can use:
temporary file (e.g. tempnam()),
cache (NoSQL: memcached, redis),
session variable ($_SESSION), but you need to start the session first.
I use extract() method to pass variable among PHP Scripts. It look like below example:
1. File index.php
<?php
$data = [
'title'=>'hello',
'content'=>'hello world'
];
extract($data);
require 'content.php';
2. File content.php :
<?php
echo $title;
echo $content;

Categories