how to store a array values into session variable in php - php

i want to store the value of array into session variable and want to use that session variable in another page for printing all the subject name but while going to print i am getting blank page.
<?php
session_start();
$colname=array();
$i=0;
while($i<10)
{
$colname[$i]=$i;
$i++;
}
echo"$colname[0]";
echo"$colname[1]";
echo"$colname[2]";
echo"$colname[3]";
echo"$colname[4]";
echo"$colname[5]";
echo"$colname[6]";
echo"$colname[7]";
echo"$colname[8]";
echo"$colname[9]";
$_SESSION['sub1']=$colname[0];
$_SESSION['sub2']=$colname[1];
$_SESSION['sub3']=$colname[2];
$_SESSION['sub4']=$colname[3];
$_SESSION['sub5']=$colname[4];
$_SESSION['sub6']=$colname[5];
$_SESSION['sub7']=$colname[6];
$_SESSION['sub8']=$colname[7];
$_SESSION['sub9']=$colname[8];
$_SESSION['sub10']=$colname[9];
?>
while using the above session variable into another page nothing get displayed

Related

How to pass variable to another page without include

Hey guys I'm trying to pass a php variable to another page. I tried it with sessions but no result.
newspaper.php
$newspaper= $newspaper['newspath'];
print_r($newspaper);
this outputs:
path/to/the/newspaper.
Now I want to use the variable in the second page.
newspaperviewer.php
echo $newspaper;
$SESSION = $newspaper;
I tried the first one but no result. The second one seems to be faulty.
Hope you guys can help me out.
Session is what you are looking for. A session variable can store a value and use this value on all pages of your project.
First thing to do is to start session on each file on your project. You can do this like this example
<?php
session_start(); //declare you are starting a session
$_SESSION['newspaper'] = "New York Times"; //Assign a value to the newspaper session
?>
On the other file you can use the value of the session by trying something like this
<?php
session_start(); //always start session don't forget!!
echo $_SESSION['newspaper'];
// This will echo New York Times
?>
Store the variable after starting session on page A, like so:
// FIRST PAGE (foo.php)
session_start();
$_SESSION['name'] = 'Jack';
Now, on the second page (or any page that you want to have access to $_SESSION, simply do the same but pull the variable.
// SECOND PAGE (bar.php)
session_start();
$name = $_SESSION['name'];
$_SESSION['name'] = null; // Or use session_unset() to delete all SESSION vars.
And that's how you pass variables using $_SESSION.
Please use this code to set session
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
You can write like this
newspaper.php
session_start();
$newspaper= $newspaper['newspath'];
$_SESSION['newspaper'] = $newspaper;
Now you can use this session variable in
newspaperviewer.php
session_start();
$newspaper = $_SESSION['newspaper'];
echo $newspaper;
session_unset(); // remove all session variables
First
newspaper.php
$newspaper= $newspaper['newspath'];
//print_r($newspaper);
session_start(); //it starts your session here
$_SESSION['newspaper']=$newspaper; //it sets a session variable named as newspaper
Second
$newspaper= isset($_SESSION['newspaper'])?$_SESSION['newspaper']:''; //checks and sets value
echo $newspaper; //outputs value
For more see session_start
http://php.net/manual/en/session.examples.basic.php
First you will need to start the session by using session_start() at the top of your page. Second, a session variable is written like this: $_SESSION['foo'].
I suggest you read these pages to get a better understanding of what's going on.
http://php.net/manual/en/reserved.variables.session.php
http://www.w3schools.com/php/php_sessions.asp

Session variable resets on refresh

I have a session variable 'touser' which resets everytime I refresh the page but my session variable for the whole session "user" does not reset.
<?php
session_start(); require("source/header.php");
require("scripts/connection/signup/db.php");
?>
<?php
session_start();
$touser=$_POST["chats"];
$_SESSION["touser"]=$touser;
echo $_SESSION["touser"];
?>
The problem is on page refresh POST values are gone and since you assign them to $touser and it used for session so every page refresh Session value is reset.
Solution is :-
<?php
session_start();
if (isset($_POST['touser'])) { // check if POST have that index or not
$touser = $_POST["chats"]; // if yes then reassign it's value
$_SESSION["touser"] = $touser; // set reassigned value to session variable
}
echo $_SESSION["touser"];// print session value (here if POST have data then new value will show otherwise old one will show)
?>
Check, if $_POST['touser'] exists.
<?php
session_start();
if (isset($_POST['touser'])) {
$touser = $_POST["chats"];
$_SESSION["touser"] = $touser;
}
echo $_SESSION["touser"];
?>
You have to check whether $_POST["chats"] is set or not and then put value into session.
session_start();
if (isset($_POST["chats"])) {
$_SESSION["touser"]=$_POST["chats"];
}
echo $_SESSION["touser"];

send array via session in php

i have this array
$banners[0] ='a';
$banners[1] ='s';
$banners[2] ='d';
$banners[3] ='f';
I shuffle this array with
shuffle($banners);
and than I have new random list, which I can show with
echo $banners[0];
echo $banners[1];
echo $banners[2];
echo $banners[3];
and this new random list I want to put in session and send to another page on my website
This doesn't work
session_start();
$_SESSION['sesion'] = $banners;
another page:
$banners[0] ='a';
$banners[1] ='s';
$banners[2] ='d';
$banners[3] ='f';
$banners = $_SESSION['sesion'];
echo $banners[0];
echo $banners[1];
echo $banners[2];
echo $banners[3];
how I can send new shuffle banners to another page with session? thans
You need session_start() at the top of every page.
You missed the statement to start the session in the 2nd page.
So you need session_start(); to be executed before you can access a SESSION variable through $_SESSION[]

get a selection from a dropdown list assigned to a variable PHP

I know I this is simple, but i just can;t find the resource that tells me how to do it.
SO my code is as follows:
session_start();
$wquery=
"select week_num,week_name
from stats_week
where season=$this_season
order by week_num";
$wresult=mysql_query($wquery);
print'<form action="changeplayer_2.php" method="post">';
print"<select name='Week_select'> <br>";
while ($wrow=mysql_fetch_array($wresult))
{
print '<option value="'.$wrow['week_num'].'">'.'week '.$wrow['week_num'].' '.$wrow['week_name'].'</option><br>\n';
}
print "</select><br><br>";#
print'<button type="submit" >Next</button>';
print"</form>";
So I am making a selection:
I want that selection to end up in: $_SESSION['week']
$_SESSION['week']=$_POST['Week_select'];
you can do this either sending data by form on submit or if you want to do without refreshing page you can do this by ajax
and than set like this
$_SESSION['week']=isset($_POST['Week_select'])?$_POST['Week_select']:someDefau‌​lt;
and to do this by ajax check this answer
Inside file "changeplayer_2.php" you'll want to load the POSTed value to the session:
session_start();
$_SESSION['week'] = $_POST['Week_select'];
Whenever you want to get the value of the session at key 'week', simply do:
$weekValue = isset($_SESSION['week']) ? $_SESSION['week'] : false;
But if you do use the session variable on any script, make sure you start the session using session_start().
To destroy the session variable, you can do:
session_start();
unset($_SESSION['week']);

How can I clear my php session data correctly?

My php website flows like this:
Page1.php has an html form which POSTs to Page2.php
Page2.php stores all the POST data into SESSION variables and has a button leading to Page3.php
Page3.php has another form which POSTs its data to Page4.php
Page4.php then stores all its POST data into SESSION variables
My problem is that it may be nessicary for a user to click the back button on Page4.php to go back to Page3.php and change some input. AS im sure your all aware when they get back to Page3.php the form will be blank as the entire page is re-rendered in its default state.
To get around this and re-display the user's previous input im doing this:
<input value="<?php echo $_POST["guest1Ticket"];?> " type="text" name="guest1Ticket" id="guest1Ticket" onblur="isTicketNumber(this)" size ="22"/>
This being the important part - <?php echo $_POST["guest1Ticket"];?>
This works but creates another problem for me. If the user goes back to Page1.php (before colsing their browser) and starts the process over again when they get to Page3.php the data from their last run through will be loaded into the form.
What I figure I need to do is clear all of the sdession variables when the user visists Page1.php. I tried to to that like this:
<?php
session_start();
session_unset();
session_destroy();
?>
(The above is at the very top of my file with no whitespace before the first character.)
No Warnings are generated when Page1.php loads but the session variables are not getting unset. When I get to Page3.php the data from the last run is still being entered into the form.
How can I clear my session data correctly?
BTW I only need this to work in Chrome and thats where im testing.
Only use session_unset() for older deprecated code that does not use $_SESSION.
see session_destroy manual
example you can try and see how it works
session.php
<?php
session_start();
$_SESSION = array('session1'=>1,'session2'=>2);
echo $_SESSION['session1']; //1
$_SESSION['session1'] = 3;
echo "<pre>";
print_r($_SESSION); //session one now updated to 3
echo "</pre>";
$_SESSION = array();
if ($_SESSION['session1']) {
echo $_SESSION['session1']; // IS NOW EMPTY
} else {
echo "woops... nothing found";
}
?>
<p>
<a href="destroyed.php">NOW GOING TO DESTROYED PHP<a/>
</p>
<?php
session_destroy();
?>
destroyed.php
<?php
session_start(); // calling session start first on destroyed.php
print_r($_SESSION); // prints Array ( )
?>
From the documentation:
If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used,
use unset() to unregister a session variable, i.e. unset ($_SESSION['varname']);
And take care about session_destroy:
session_destroy destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session
Use session_unset(). Like this:
<?php session_start(); ?><!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["variabletounset"] = "I am going to be unset soon along with all of the other session variables.";
print '<pre>' . "\n";
print_r($_SESSION);
print ' </pre>' . "\n";
session_unset();
print ' <pre>' . "\n";
print_r($_SESSION);
print ' </pre>' . "\n";
?>
</body>
</html>
This would output:
Array
(
variabletounset => I am going to be unset soon along with all of the other session variables.
)
Array
(
)

Categories