Jump to content

Archived

This topic is now archived and is closed to further replies.

AdRock

Problem with echoing session [RESOLVED]

Recommended Posts

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
[code]<? 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>[/code]

This is the first part of the edit_profile form where the variable don't appear
[code]<?
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;

}[/code]

just in case you need it here is the register session variables
[code]       // 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;[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Probably.  If you could post the script where the sessions are set that would help :D

Share this post


Link to post
Share on other sites
Here is the login script which works
[code]<?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();
  }
}
?>[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
You would use something like this in your display for each form field:

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

Assuming that each of those variables is already set.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.