So I wonder if it is possible to get a variable from a specific php-file when the variable-name is used in multiple php-file.
An example is this:
<header>
<title>
<?php echo $var1; ?>
</title>
</header>
page1.php has $var1 = 'page1'
page2.php has $var1 = 'page2'
footer.php should have $var1 from page1$var1 from page2
Ok the example is a bit abstract, but as short as I can make it. I think you get what I am getting at! So it is the in the footer I am after! Got any solutions?
You can, but the variable in your last include will overwrite the variable in your first one:
myfile.php
$var = 'test';
mysecondfile.php
$var = 'tester';
test.php
include 'myfile.php';
echo $var;
include 'mysecondfile.php';
echo $var;
Output:
test
tester
I suggest using different variable names.
You could also use a session for passing small bits of info. You will need to have session_start(); at the top of the PHP pages that use the session else the variables will not be accessable
page1.php
<?php
session_start();
$_SESSION['superhero'] = "batman";
?>
Go to the other page
page2.php
<?php
session_start(); // this NEEDS TO BE AT THE TOP of the page before any output etc
echo $_SESSION['superhero'];
?>
using include 'page1.php' in second page is one option but it can generate warnings and errors of undefined variables.Three methods by which you can use variables of one php file in another php file:
use session to pass variable from one page to anothermethod:first you have to start the session in both the files using php command
sesssion_start();
then in first file consider you have one variable $x='var1';
now assign value of $x to a session variable using this:
$_SESSION['var']=$x;
now getting value in any another php file:
$y=$_SESSION['var'];//$y is any declared variable
using get method and getting variables on clicking a linkmethod
clickme
getting values in page2.php file by $_GET function:$x=$_GET['variable1'];//value1 be stored in $x$y=$_GET['variable2'];//vale2 be stored in $y
if you want to pass variable value using button then u can use it by following method:
$x='value1'<input type="submit" name='btn1' value='.$x.'/>
in second php $var=$_POST['btn1'];
You could also use file_get_contents
$url_a="http://127.0.0.1/get_value.php?line=a&shift=1&tgl=2017-01-01";
$data_a=file_get_contents($url_a);
echo $data_a;
Related
I hope this isn't stupidly simple. I am completely new to web dev.
I have list items that I styled as buttons.
I want to be able to link to a new page as well as store some information when the list items are clicked. I want to be able to store which list item was clicked in a Session variable.
How do I accomplish this/ is there a better way to accomplish the same thing?
Sessions Step By Step
1- Defining session before everything, Ideally do it on very top of the document so no output is being exposed etc like this
<?php
session_start();
?>
2 - Set your session inside a page and then you have access in that page. For example this is page 1.php
<?php
//This is page 1 and then we will use session that defined from this page:
session_start();
$_SESSION['danish']='danish';
?>
3- Using and Getting session in 2.php
<?php
//In this page I am going to use session:
session_start();
if($_SESSION['name']){
echo 'Your name variable Is Here! :) ';
}
?>
In short its like you assign session variable in a page and then using same declarative syntax instead of assigning you call the variable and PHP do the magic to check if that session variable was created and hold the value so, in short i can write my code like this
First Page
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
Second page
<?php
session_start();
echo $_SESSION['myvar'];
?>
So I wonder if it is possible to get a variable from a specific php-file when the variable-name is used in multiple php-file.
An example is this:
<header>
<title>
<?php echo $var1; ?>
</title>
</header>
page1.php has $var1 = 'page1'
page2.php has $var1 = 'page2'
footer.php should have $var1 from page1$var1 from page2
Ok the example is a bit abstract, but as short as I can make it. I think you get what I am getting at! So it is the in the footer I am after! Got any solutions?
You can, but the variable in your last include will overwrite the variable in your first one:
myfile.php
$var = 'test';
mysecondfile.php
$var = 'tester';
test.php
include 'myfile.php';
echo $var;
include 'mysecondfile.php';
echo $var;
Output:
test
tester
I suggest using different variable names.
You could also use a session for passing small bits of info. You will need to have session_start(); at the top of the PHP pages that use the session else the variables will not be accessable
page1.php
<?php
session_start();
$_SESSION['superhero'] = "batman";
?>
Go to the other page
page2.php
<?php
session_start(); // this NEEDS TO BE AT THE TOP of the page before any output etc
echo $_SESSION['superhero'];
?>
using include 'page1.php' in second page is one option but it can generate warnings and errors of undefined variables.Three methods by which you can use variables of one php file in another php file:
use session to pass variable from one page to anothermethod:first you have to start the session in both the files using php command
sesssion_start();
then in first file consider you have one variable $x='var1';
now assign value of $x to a session variable using this:
$_SESSION['var']=$x;
now getting value in any another php file:
$y=$_SESSION['var'];//$y is any declared variable
using get method and getting variables on clicking a linkmethod
clickme
getting values in page2.php file by $_GET function:$x=$_GET['variable1'];//value1 be stored in $x$y=$_GET['variable2'];//vale2 be stored in $y
if you want to pass variable value using button then u can use it by following method:
$x='value1'<input type="submit" name='btn1' value='.$x.'/>
in second php $var=$_POST['btn1'];
You could also use file_get_contents
$url_a="http://127.0.0.1/get_value.php?line=a&shift=1&tgl=2017-01-01";
$data_a=file_get_contents($url_a);
echo $data_a;
I created a session variable in a php page 'session1.php' as follows
<?php
session_start();
$_SESSION['name']="piklu";
?>
now I want to access the session value in another page say 'session2.php'.i need suggestion for that.
what I have done in 'session2.php' is as follows.
<?php
include('session1.php');
echo $_SESSION['name'];
?>
The above code is working but only when session1.php and session2.php doesn't contain any html tags.if it is then all html tags from 'session1.php' will be copied to 'session2.php'. Is there any other way to do that?
You don't need to include all of session1.php. instead, just call session_start() at the start of session2.php.
I have a action.php which does some processing involving a MySQL-Database. In action.php I generate a variable
$author
with a string in it. When the script terminates it calls test.php by
header('Location: ../test.php');
Now while test.html is shown, I want to display the content of the stringvariable
$author
in a html-element. Like
<h2>echo $author;</h2>
How do I achieve that? Thank you for any responses in advance.
You could put the contents of the $author in a session:
<?php
// action.php
session_start();
// Your code here
$_SESSION['author'] = $author;
// Redirect to test.php
<?php
// test.php
session_start();
echo '<h2>'. $_SESSION['author'] .'</h2>';
See:
session_start
you could store $author in a session variable or on the action.php page output a form with a hidden input with the value of $author and then submit it to test.php
to use session variables don't forget session_start(); and then $_SESSION['author'] = $author
In your action.php save your variable in session like this: $_SESSION['author'] = $author;
Then, in your test.php file you can use <h2><?php echo $_SESSION['author']; ?></h2>
Don't forget to start both .php files with calling session_start();
Use a template engine, e.g. Twig.
While you can use PHP itself as a template engine (include() your file and use <?=$var?> or <?php echo $var; ?> in it), using a real template engine is usually nicer as you won't even think about moving actual business logic into your templates when you have a good template engine.
You'll need to either store the value of $author in $_SESSION or in a cookie.
See session.php
Sounds like you are trying to pull author data from a database and display it in test.php. If so there is no need to "pass" the data to test.php, just grab data in test.php.
test.php
<?php
// Open DB handle
// Do query, get results.
// Store results in array ($aRes)
// Close DB handle
?>
<html>
.
.
.
<body>
<h2><?php echo $aRes['author'];?></h2>
.
.
</body>
</html>
That is a very basic and obviously fairly pseudo template but hopefully it will give you a better idea of the relationship between server-side data and HTML.
I want to include a page that contains a variable with a value that I need to insert in a database, but when I include the page, functions out of the scope of the second page try to run, which leads to an undefined error.
Basically I want this:
mainpage.php
<?php
$variable = 'value';
function();
?>
secondpage.php
<?php
include 'mainpage.php';
echo $variable;
?>
But I cant do this without also calling the function.
PHP executes statements line-by-line, so you cannot control what it does unless your mainpage.php has some conditional statements that you can influence from within secondpage.php.
Inside function type,
global $variable;
So your page will look like this:
$variable = 'value';
function();
echo $variable;
So if you want to function() to access $variable, you need to put global $variable inside function, echo $variable should echo "value".