PHP-General - $_SESSION variable value not getting displayed
Discuss $_SESSION variable value not getting displayed in the PHP Development forum on Dev Shed. $_SESSION variable value not getting displayed PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 30
Time spent in forums: 6 h 17 m 41 sec
Reputation Power: 1
PHP-General - $_SESSION variable value not getting displayed
Hi!
I am a relative newbie to PHP. I learnt PHP on my own by studying PHP programs and reading as much as I could on the net.
I would like some help on one of the basic issues.
I have written a PHP script that welcomes the user after successful login. The name of the user is displayed too after login. I have used $_SESSION variables for storing username, a code (that needs to be displayed) and have displayed it on the welcome page (part of the script below). The username gets displayed properly in the following script even the code is stored in the $_SESSION variable too but funnily isn't displaying!! If the code wasn't stored, then the reports that run using the code would have failed but the reports run too!! So I know that the code is stored properly in $_SESSION variable but when I want to display it on the welcome page it just doesn't display!! By the way, this happens especially when I run this script from a web server. From my local PHP server (on my dev platform), the script works perfectly!! It is weird that without getting ANY errors as such (like variable undefined etc.) I am unable to show this code on the welcome page. The code display is needed to let the user know that he/she can run the reports ONLY by providing this code for running reports as a parameter. I am clueless as to what could be wrong! The following code shows username stored properly by using $_SESSION['name'] directly! But not $_SESSION['cd']!! though the value exists for it (else would have got the error in the report)!!!!
Please help! Could it be the issue of web-server not configured properly for PHP? Why are session variables creating problems only on a web server and not on a local server? Are these variables getting properly destroyed after the session ends? How do I find out?
Any help will be greatly appreciated in this matter!
Thank you in advance!
Mozart66
PHP Code:
<?php
session_start();
$mcode = $_SESSION["cd"];
echo $mcode;
....
....
....
....
if($_SESSION['logged_in']){
....
....
....
echo "<td width='75%' bgcolor='#D5D2D2' valign='top'>";
echo "<br><br><p align='center'><strong>LOGIN SUCCESSFUL!!</strong></p><br>";
echo "<p align='center'><strong>HELLO ".$_SESSION['name']."!</strong></p><br>";
echo "<p align='center'><strong>WELCOME TO</strong></p><br>";
echo "<p align='center'><strong>BSSK - EDUCATIONAL SPONSORSHIP DATABASE</strong></p><br><br>";
echo "<p align='center'>Your <strong>Code</strong> is <strong>".$mcode."</strong></p><br><br>";
echo "<p align='center'><strong>Please use above mentioned appropriate code, as designated, for entering and updating Child and Sponsor records. It is IMPORTANT.</strong></p><br><br><br>";
echo "<p align='left'><strong>NOTE:</strong> Click on appropriate option from the left to perform the task.</p>";
echo "</td>";
} else {
echo "<td width='25%' valign='top'> </td>";
echo "<td width='75%' bgcolor='#D5D2D2' valign='top'>";
echo "<br><br><br><p align='center'><strong>LOGIN UNSUCCESSFUL!</strong></p><br>";
echo "<p align='center'><strong>TRY LOGGING IN AGAIN</strong></p>";
echo "<p align='center'><strong>NOTE: </strong>Click here on <a href='eslogin.php'><strong>Login</strong></a> to log in to the system.</p><br>";
echo "</td>";
}
...
...
?>
Posts: 1,833
Time spent in forums: 1 Month 2 Weeks 1 Day 43 m 31 sec
Reputation Power: 811
The "cd" vs 'cd' is simply nonsense. Both literals create the exact same string (read the PHP manual on when double quoted and single quoted strings do differ).
We need more specific info. Give us the code where you generate the code and store it in the session. Make a var_dump() of the session right after session() start. I'm pretty sure there's some logical error, because session values usually don't just disappear.
You can also read the session. Open your php.ini to see where the session files are stored (session.save_path) and then open them. They're in a human readable format.
Posts: 377
Time spent in forums: 1 Week 3 h 27 m 44 sec
Reputation Power: 293
maybe try doing a var_dump or print_r of $row after to check that the data is coming back from the DB as you expect.
also as a side note, you are wide open to sql injection with that code. Never trust post or get data, it can very easily be modified by a user to force a login, or dump usernames / passwords / emails etc.
look into using PDO and prepared statements or at the very least sanitise your inputs.
__________________
"Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe
Posts: 2,475
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 8 m 48 sec
Reputation Power: 2194
I realize you learned PHP on your own, probably using some horrific tutorial that pops up at the head of the Google search results, so please understand I mean this in the best possible way:
Your code is horrible. It is wrong and full of security holes.
Please ditch what you're doing and start over, using this tutorial as your guide.
__________________
I ♥ ManiacDan & requinix
This is a sig, and not necessarily a comment on the OP: Please don't be a help vampire!
Posts: 30
Time spent in forums: 6 h 17 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by sir_drinxalot
maybe try doing a var_dump or print_r of $row after to check that the data is coming back from the DB as you expect.
also as a side note, you are wide open to sql injection with that code. Never trust post or get data, it can very easily be modified by a user to force a login, or dump usernames / passwords / emails etc.
look into using PDO and prepared statements or at the very least sanitise your inputs.
Thank you so much for the good input! I am a newbie so I appreciate this help greatly! I have sanitized my inputs in my starting script (which I haven't included here) and now the script seems to be working on its own after I tried doing var_dump of $row and $_SESSION variables!!! Now I am getting the result exactly as I want it to be! The code is also getting displayed on the welcome page! All I did was just added var_dump, tested it and once the results could be seen, I removed it and tested it again! It worked like a charm!!!
THANK YOU ALL! Have learnt some new things such as var_sump, print_r, PDO! I am learning PHP on my own and hence your guidance is very valuable to me!
Since I have got my answer, the mods may close this thread!