I've written a bit of code to upload a file to a server. When the file is successful I made my code go to another web page. On this web page I want to print the file properties which were gained from the previous page so I am using Sessions in PHP.
//Starts up a new PHP session
session_start();
$_SESSION['file']=$_FILES["file"];
$_SESSION['name']=$_FILES['name'];
$_SESSION['type']=$_FILES['type'];
$_SESSION['size']=$_FILES['size'];
$_SESSION['tmp_name']=$_FILES['tmp_name'];
That is my session on page1. Then, when the file is successful, I send the user to page2.
header( 'Location: page2' ) ;
Now, on page2, I have this right at the top of my .php page:
<?php
//Starting session
session_start();
?>
Then, further down for me to be able to print each variable out I've got:
<?php
Print_r ($_SESSION['file']);
?>
I get all my information all jumbled into a long sentence when this is done. I want to have control over the information and print it nice and neatly. What am I doing wrong? I've researched into loads of different ways of doing this and nothing has helped so far.
Tried that, also tried just printing SESSION, also tried echoing each one seperately with the words [Array] being printed only.
Thanks in advance!
Use two dimensional array like below
$_SESSION['upload1']['file']=$_FILES["file"];
$_SESSION['upload1']['name']=$_FILES['name'];
$_SESSION['upload1']['type']=$_FILES['type'];
$_SESSION['upload1']['size']=$_FILES['size'];
$_SESSION['upload1']['tmp_name']=$_FILES['tmp_name'];
on file2.php
<?php
print "<pre>";
print_r($_SESSION['upload1']);
print "</pre>";
?>
You are dealing with white-space breaks vs HTML breaks.
Just print it as <pre> and you're good to go.
<pre>
<?= print_r ($_SESSION['file']); ?>
</pre>
print_r() will print out your information in a nice ASCII-Art like format. The HTML engines render this unreadably. Chose "view source" (Ctl-U) in your browser to view it in a human-understandable form.
You can also install Xdebug which will make really nice formatting : http://xdebug.org/docs/install
and see: https://stackoverflow.com/search?q=how+to+install+xdebug if you have any issues installing xdebug
Use a table to display the data (or am i missing something !?!) :
<table>
<tr><td>File Name</td><td><?php echo $_SESSION['name'] ?></td></tr>
<tr><td>File Size</td><td><?php echo $_SESSION['size'] ?></td></tr>
</table>
add whatever extra info you need ...
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'];
?>
This is more of a general question without specific code.
Under what circumstances can a $_SESSION variable in PHP simply vanish?
I have properly declared
<?php session_start(); ?>
without spaces at the very top of each page. On one page, I can refer to the session variable and everything's OK. Yet, when I go to the page that's a problem, and use
<pre> <?php print_r($_SESSION); ?> </pre>
I get no results - the session is empty.
Am I missing something obvious?
SOLVED. It had to do with the fact that there was a problem <body> tag (and /body) in the wrong place. Once moved/removed, it works.
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 want to print a page using javascript in php while loop. Please help me by giving an advice.
eg:
$q=mysql_query("SELECT name, address FROM member_table);
while ($r=mysql-fetch_row($q)){
echo $r['0']."<br />".$r['1'];
echo 'javascript:window.print()';
}
But this is not working. Please help me.
I want to print the page similiar to Print But this should be automatic as while condition will bring more result in looping process
Is there any way to send the page directly to printer one by one from loop or other method. Please help me by giving an example
Could you provide more details on what you're trying to do. PHP is server side while javascript is client side. When php is done producing the page (loops or not) you still have just 1 page rendered. I am not sure what javascript:window.print() in the loop gives you.
If you want to trigger the page to print after the whole thing is rendered, you can put
<script>
javascript:window.print();
</script>
at the bottom of your page but not in your loop.
Don't use echo for javascript output.
<?php while { ?>
Javascript here
<?php } ?>
I have a form that is supposed to store some of its data into a session. For the purpose of this example.
This information is then collected on another page.
<?php
session_start();
print_r($_SESSION['booking-form']); ########### for debugging purposes ###########
if ($_SESSION['booking-form']) {
echo $_POST['GT_title']
?>
<!DOCTYPE html>
<html>
<?php
}
elseif ($_SESSION['booking-form']) {
}?>
</html>
Now although the debug is working the echo is not working ie. echo S_POST['GT_title'].
How do I echo the information on the second page.
Can I echo it wherever I want it in the html (ie. in the middle of the body somewhere with
On the first page some data in the form can be changed with options. This would need to update the session before the page is changed.
CLARIFICATION
For the purpose of a booking form. The user filled in a series of options that were then echo'd on the next page. At this point they need to log in so the form data must enter into a session. The session data should then be returned on a third page.
1) what information? $_POST['GT_title'] would refer to a form element, presumably on the previous page. Does it exist? Is it filled in? what are you expecting?
2) You can echo where ever you like.
3) clarify? you can update a session with post values easily.
$_SESSION['foo'] = $_POST['bar']
your question isn't really that clear.
You can echo it wherever you want but if it doesn't exist or doesn't have a value then it might appear to be not working. To know for sure use var_dump($_POST['GT_title']);
For your echo question: Are you sure that GT_title is set? Posting the form on your previous page might help.
You can echo wherever you want, you can just put
<?=$varname?>
anywhere in your html.
You can also update your session wherever you want.
$_SESSION['abc'] = value;
echo '<pre>';
print_r($_POST);
echo '</pre>';
Print it after your header.
I hope this helps a little :)