help with understanding sessions and practical/correct usage

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

The Ninja Space Goat wrote:As for a PHP editor... syntax highlighting will prevent 99% of parse errors... here's a free one that's pretty decent:
http://crimsoneditor.com/
SWEET!!! thanx goat :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

just fyi... you need to quote your array indeces... $_SESSION[logname] should be $_SESSION['logname']. I would develop with errors set to E_ALL so you can see errors that this would normally produce.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

done in the beginning of my new code i have

Code: Select all

session_start();
ini_set('error_reporting', E_ALL); 
ini_set('display_errors', 1);
and i took your advice on cleaning it up and so i tried both this

Code: Select all

<div style=\"padding-left:17px; padding-top:20px;\"><?php echo "$_SESSION['logname']welcome to the 
secret page!"; ?></div>
with this i get the error message
Parse error: parse error, unexpected T_VARIABLE in C:\Program Files\xampp\htdocs\Log_In\agent\index_new.php on line 43
and then this way

Code: Select all

<div style=\"padding-left:17px; padding-top:20px;\">$_SESSION['logname']welcome to the secret 
page!</div>
and i get this error
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\xampp\htdocs\Log_In\agent\index_new.php on line 43
is there a different way of going about this that i should be using?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

within double-quoted strings you can either leave the ' out or encapsulate the whole term with { }

Code: Select all

echo $_SESSION['logname'];
// but
echo "... $_SESSION[logname] ...";
// or
echo "... {$_SESSION['logname']} ...";
// or
$tmp = $_SESSION['logname'];
echo <<< eot
   yadda yadda
   yadda $tmp yadda
eot;
Obadiah wrote:

Code: Select all

<div style="padding-left:17px; padding-top:20px;"><?php echo "$_SESSION['logname']welcome to the 
secret page!"; ?></div>
You never open another php block within another php block.



Do you have plans on using $display_block in a more ...complex context than

Code: Select all

<html>
<head>
<title>Secret Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
?
If not then there's no reason that variable at all.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

volka wrote:within double-quoted strings you can either leave the ' out or encapsulate the whole term with { }
wel well... you learn something every day... thanks volka.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

i thought that was the reason one would use display block....to send blocks of html in a php script for viewing in the browser....should i have used just echo? right now all im doing is referencing what i learn here, to what i have read and the examples ive tried in my books...was there another way of going about it? eventually what im going to need to do is have a page of links that will link the user to a certain folder that will only have his information...
besides posting the info in the database on the pages based on the user_id or the logname....can i use something like what i did here for that

Code: Select all

//i hate trying to wing this stuff  but its the only way i know how to explain what im tryna do
 
$next_program = ($sql = "SELECT user_name FROM $table_name 
                        WHERE user_name='$_POST[fusername]' ==_SESSION['logname']";"/Credit_Info.php";)
and say if the users logname is joe when he clicks on the link it will automatically take him to his folder that
has that file
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

why take him to his own folder... why not just take him to a template that gets filled in with is data from a database?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Obadiah wrote:i thought that was the reason one would use display block....to send blocks of html in a php script for viewing in the browser....should i have used just echo?
I don't understand; you're mixing php and html already

Code: Select all

<html>
<head>
<title>Secret Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
At this state I don't see any advantage over

Code: Select all

<?php session_start(); ?>
<html>
<head>
  <title>Secret Page</title>
  <link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
[... html code ...]
                              <td valign="top" width="208" height="302">
                                <div style="padding-left:17px; padding-top:20px">
                                  <?php echo $_SESSION['logname']; ?>welcome to the secret page!
                                </div>

                                <div style="padding-left:22px; padding-top:19px">
                                  <img src="images/2_p1.gif" alt="">
                                </div>
[... html code ...]
</body>
</html>
There might be ...but not yet.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

The Ninja Space Goat wrote:why take him to his own folder... why not just take him to a template that gets filled in with is data from a database?
ok...imagin you were a independent sales contracter and you wanted to login to this site to see your
information....now, heres the thing....my boss is not wanting them to be able to change amounts and figures
since that would really suck....so he says to me when they log in, and they click on a link, it will bring them to a page that will have something like a drop down of months where their able to view a report of all their personal sales for that month...the report will be a pdf...basically a image

will something like this work?

Code: Select all

<a href="{$_SESSION['logname']}"report.pdf">
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

holy <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> .....lol...it does work!!!! it takes me to the path but not the file

Code: Select all

<a href=\"{$_SESSION['logname']}\"report.pdf\">
it ignores report.pdf....why?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

look at the source where that link is... that should tell you EXACTLY why (hint: quote)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Sending a "static file" or the output of a script doesn't make any difference for the client.
e.g. a php script can create a pdf document on the fly and vice versa can a "static" pdf file be altered on the client-side. I put static in quotes because there is no such thing in terms of a http connection; the output of a php script is sent exactly the same way as the contents of e.g. report.pdf or report.tif or whatever. Everything you send to the client can be altered there - you have no control over the raw data the client receives.
Depending on the importance of the data you might want to consider http://en.wikipedia.org/wiki/Digital_si ... entication
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

right they can alter the copy of the pdf but as far as altering the excel file pasted as a image....they cant touch it....thats the whole idea behind it we give them a image of the file....we still have the original file

:oops: i feel bad about having the backslash in the link...that sucked
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You can "give" them the excel file as well. Or a view of the db data cresated by a php. And still you keep the "original" data.
Really, there's no point to it ;)
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

ok....what if im wanting to log a user out...i was reading and came across something like

Code: Select all

session_destroy();
but how do i put that into a link like something we have here on the boards?
Post Reply