Jump to content


Photo

Problem with echoing session [RESOLVED]


  • Please log in to reply
5 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 11 August 2006 - 12:00 PM

I have a problem with the session variables not being echoed when requested on one of my pages

After the user logs in successfully they get a greeting using the session variable (so I know it works) but when i move onto a different page the session variable are'nt echoed.

I have a form which should echo the $_session['first_name'], $_session['last_name'] and $_session['email_address'] but none of them are echoed

This is the my_profile page which does echo the varibles
<? session_start(); ?>

<h2>My Account Settings</h2>
<p class="style2">Welcome <? echo $_SESSION['username'] . $_SESSION['first_name'] . $_SESSION['last_name'] . $_SESSION['email_address']; ?></p>
<p class="style3">'My Account Settings' allows you to manage your account preferences.<br>
Under 'Update my personal details' you can change your email address.<br>
Under 'Change my account password' you can change your password.</p> 
<ul>
<li><a href="http://www.mysite/index.php?page=edit_profile">Update my personal details</a></li>
<li><a href="http://www.mysite/index.php?page=change-password">Change my account password</a></li>
</ul>

This is the first part of the edit_profile form where the variable don't appear
<?
session_start();

if(!isset($session['userid'])){
echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>";
exit;
}

// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";

// Convert to simple variables 
$first_name = stripslashes($_POST['first_name']); 
$last_name = stripslashes($_POST['last_name']);
$email_address = stripslashes($_POST['email_address']);

if (!isset($_POST['first_name'])) {
?>
<h2>Update personal details!</h2>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

	<p class="style3"><label for="first_name" style="width:8em">First Name:</label>
        <input type="text" title="Please enter your first name" name="first_name" size="30" value="<? echo $_SESSION['first_name']; ?>"/></p>

        <p class="style3"><label for=last_name" style="width:8em">Second Name:</label>
        <input type="text" title="Please enter your last name" name="last_name" size="30" value="<? echo $_SESSION['last_name']; ?>"/></p>

        <p class="style3"><label for="email_address" style="width:8em">Email address:</label>
        <input type="text" title="Enter your email address" name="email_address" size="30" value="<? echo $_SESSION['email_address']; ?>"/></p>		

</form>
<?php
}
elseif (empty($first_name) || empty($last_name) || empty($email_address))  {

    echo $empty_fields_message;

}

just in case you need it here is the register session variables
       // Register some session variables! 
        session_register('first_name'); 
        $_SESSION['first_name'] = $first_name; 
        session_register('last_name'); 
        $_SESSION['last_name'] = $last_name; 
        session_register('email_address'); 
        $_SESSION['email_address'] = $email_address;
        session_register('username'); 
        $_SESSION['username'] = $username; 
        session_register('special_user'); 
        $_SESSION['user_level'] = $user_level;

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 11 August 2006 - 05:27 PM

I have noticed the only session variable that can be echoed is the $_SESSION['username']

Does that mean none of the other session variables have been registered?
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#3 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 05:32 PM

Probably.  If you could post the script where the sessions are set that would help :D
Why doesn't anyone ever say hi, hey, or whad up world?

#4 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 11 August 2006 - 10:37 PM

Here is the login script which works
<?php
session_start();  // Start Session
session_register("session");

// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";

// Convert to simple variables 
$username = $_POST['username']; 
$password1 = $_POST['password1'];

if (!isset($_POST['username'])) {
?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <p class="style3"><label for="username" style="width:8em">Username:</label>
    <input type="text" title="Please enter your username" name="username" size="30"/></p>

    <p class="style3"><label for="password1" style="width:8em">Password:</label>
    <input type="password" title="Please enter your password" name="password1" size="30"/></p>

    <label for="links" style="width:8em"></label>
    <a href="http://www.project-sw.co.uk/jack/index.php?page=register">New Member Registration</a>&nbsp&nbsp&nbsp<a href="http://www.project-sw.co.uk/jack/index.php?page=forgot-password">Forgtten Password</a>

    <p class="style3"><label title="Login">
    <input type="submit" value="Login" style="margin-left:97px" class="submit-button"/></label></p>
</form>
<?php
}

elseif (empty($username) || empty($password1))  {

    echo $empty_fields_message;

}

else {
$password1 = md5($password1); 

include 'includes/connection.php';

// check if the user info validates the db 
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password1' AND activated='1'"); 
$login_check = mysql_num_rows($sql); 

if($login_check > 0){ 
    while($row = mysql_fetch_array($sql)){ 
    foreach( $row AS $key => $val ){ 
        $$key = stripslashes( $val ); 
    } 
        // Register some session variables! 
        session_register('first_name'); 
        $_SESSION['first_name'] = $first_name; 
        session_register('last_name'); 
        $_SESSION['last_name'] = $last_name; 
        session_register('email_address'); 
        $_SESSION['email_address'] = $email_address;
        session_register('username'); 
        $_SESSION['username'] = $username; 
        session_register('special_user'); 
        $_SESSION['user_level'] = $user_level; 
         
        mysql_query("UPDATE users SET last_login=now() WHERE username='$username'"); 
        include "includes/newsession.php"; 
        echo "<h2>Welcome ". $_SESSION['first_name'] ." ". $_SESSION['last_name'] ." ". $_SESSION['username'] ." ". $_SESSION['email_address'] . "!</h2>  
    	You have successfully logged in!<br /><br />You will now be redirected back to the Jack Godfrey Honeylands Support Fund home page.  If you are not redirected in the next 3 seconds click <a href='http://www.project-sw.co.uk/jack/index.php'>here</a>";
	echo" <meta http-equiv='refresh' content='3;url=http://www.project-sw.co.uk/jack/index.php'>"; 
    } 
} else { 
    echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br /> 
    Please try again!<br />"; 
    echo "Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
    session_unset(); 
   }
}
?>

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#5 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 12 August 2006 - 03:59 PM

I have found out what was causing the sessions not to be displayed.  It was :

$first_name = stripslashes($_POST['first_name']);
$last_name = stripslashes($_POST['last_name']);
$email_address = stripslashes($_POST['email_address']);

How do I get the session variable or even the record in mysql to display in the form so they can be updated?
The purpose of this is the user may change their email address and I want to show their their current email.
If i take out or comment out the above the session variables are shown in the text boxes.

Would it be better to connect to the database and select the record and display it rather than using the session?
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#6 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 12 August 2006 - 04:44 PM

You would use something like this in your display for each form field:

<INPUT TYPE="text" name="bozo" VALUE="<? echo $bozo ?>"/>

Assuming that each of those variables is already set.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users