Using php session variable in different pages - php

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.

Related

Storing Session information and linking to new page on click

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'];
?>

I have two PHP files. In my 1st PHP file I have a variable named "url" (which was taken from another HTML page) [duplicate]

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;

How to syntax the session_start(); if in a separate php file

I have a contact form that links to an external php file (because I have a single-scrolling page with all content on it, ran by jQuery and it wouldn't make sense to have it all as a .php file as would be the case with a single, separate contact page).
My question is, since I have a captcha to protect against spam and such, I have to include the session_start(); in both the contact php file and the captcha. My question is would I syntax it in the contact.php file as:
<?php session_start(); ?>
and then have more php here, i.e. - my contact form script
?>
or just have it be the first line as:
session_start();
after the initial php tags?
Thanks for the help.
You need to include session_start(); at top of all your php codes to apply a session.
<?php
session_start();
$user='New User';
$_SESSION['user']=$user;
//Print this line on another php code you will get the same username
echo "Welcome $_SESSION['user']";
?>
Question is not clear. You should probably try this code at the top of the page.
<?php
#session_start();
?>
check this link
http://www.php.net/manual/en/function.session-start.php
or this Load session_start() only if session does not exist?

How to transfer php session variables from a page to an iframe inside the page

So I have a webpage with session variables, for example one variable is:
$_SESSION['name'] = 'testname';
I have an iframe within the page, and want to use this session variable in it. For a start, I am trying to just do this:
echo $_SESSION['name'];
I have not found a way to transmit the session variable into the iframe page.
INFORMATION (IMPORTANT!):
I have used session_start(); on both the main page and the iframe page, and I have also tried using session_write_close(); on both pages. The pages are in the same domain. Please answer. Thank you in advance!
What ever page is referenced in the iframe, if it exists on your the same domain will have access to the same session information on the hosting site so long as that page has session_start() called on it.
Example.
page1.php
<?php
session_start();
$_SESSION["HELLO"] = "WORLD";
?>
<html>
<iframe src='page2.php'/>
</html>
page2.php
<?php
session_start();
echo "HELLO ".$_SESSION["HELLO"]; // will output HELLO WORLD
This is only true for sites that have access to the same cookies and the same session store.

PHP, getting variable from another php-file

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;

Categories