Jump to content

session variables help


scmeeker

Recommended Posts

I'm trying to get the username from a login form to carry over to the next page and have the username show on top of the next using the session variables.  I'll show my page in sections:

 

At the top of the php part I have:

 

<?php
session_start();
session_register ("username");
$HTTP_SESSION_VARS ["username"] = $username;

 

Then at the top of the html section I have:

 

<?php echo "$username" ?>

 

There are no errors but nothing is showing up when I login successfully.  Any ideas?  Any help with session variable is greatly appreciated because I'm new to PHP and have been looking and looking on the web without success.

 

Thanks!

Link to comment
Share on other sites

Oh, you still need to have session_start() at the beginning of each script that will use $_SESSIONS. Sorry about that.

 

'value' is that value that is being assigned to $_SESSION['username']. It can be whatever you need it to be, just like any other variable.

Link to comment
Share on other sites

Okay, at the top of my page I put:

 

<?php

session_start();

$_SESSION['username'] = 'username';

because I assume that the value is "username"? 

 

And then in the HTML code I put:

 

<?php echo $_SESSION['username']; ?>

 

When this is in, it simply prints out "username" literally and not the actual username.  Hmmmm...and I've tried it several different ways....without luck.

Link to comment
Share on other sites

Okay, I tried that without luck still.  Here is the code for part of the user login form dealing with the input values:

 

<form name="form1" method="post" action="checklogin.php">

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>

<td colspan="3"></td>

</tr>

<tr>

<td width="78">Username</td>

<td width="6">:</td>

<td width="294"><input name="username" type="text" id="username"></td>

</tr>

<tr>

<td>Password</td>

<td>:</td>

<td><input name="password" type="password" id="password"></td>

</tr>

<tr>

<td> </td>

<td> </td>

<td><input type="submit" name="Submit" value="Login"></td>

</tr>

 

And then at the top of the next page I've put:

 

<?php
session_start();
$_SESSION['username'] = $username;

 

and in the HTML section put: 

 

<?php echo $_SESSION['username'];?>

 

and I also tried:

 

<?php echo $_SESSION[$username];?>

 

and: 

<?php echo $username;?>

 

with none of those working yet. 

Link to comment
Share on other sites

I'm still having no luck with this.  I'm going to post more, as I realized I didn't post another file that may help.

 

Here is the user login form:

 

<?php session_start();
$_SESSION['username'] = $_POST['username'];?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Art</title>
<link href="styles/lery2.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a:link {
color: #09C;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #09C;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: none;
}
-->
</style></head>
<div align="center">
<body>


<div class="mainbody">
  <div class="header1"></div>
  
  <div class="storeeditleftsidebar"><br />
<br />


</div>
  <br />
<br />
<br />
<br />
<span class="titlefont">Member sign in </span><br />
<span class="blackfont"><br />
sign in here. </span><br />
<br />
<table width="300" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" id="username"</td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

 

Then the reference file for the form where it says"action = checklogin.php" is here:

 

<?php
session_start();



$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="artist"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:listtest5.php");
}
else {
header ("location:userlogin_error.php");
}
?>

 

Then there is the page where the session variable "username" is being sent and printed:

 

<?php
session_start();



//connect to database
$mysqli = mysqli_connect("", "", "", "");



  //Set the selected category
$selected_cat = (isset($_GET["cat_id"])) ? $_GET["cat_id"] : false;

//show categories first
$get_cats_sql = "SELECT cat_id, cat_title FROM category ORDER BY cat_id";
$get_cats_res =  mysqli_query($mysqli, $get_cats_sql) or die(mysqli_error($mysqli));

if (mysqli_num_rows($get_cats_res) < 1)
{
   $categoryList = "<p><em>Sorry, no categories to browse.</em></p>";
}
else
{
    //Display the categories
    while ($cats = mysqli_fetch_array($get_cats_res))
    {
        $categoryList .= "<a href=\"{$_SERVER['PHP_SELF']}?cat_id={$cats['cat_id']}\">{$cats['cat_title']} <br /></a>\n";
        if ($cats['cat_id']==$selected_cat)
        {
            //get items
            $get_items_sql = "SELECT id, photo, username, title, price, date FROM product WHERE cat_id = '{$selected_cat}' ORDER BY date $pages->limit";
            $get_items_res = mysqli_query($mysqli, $get_items_sql) or die(mysqli_error($mysqli));
            if (mysqli_num_rows($get_items_res) < 1)
            {
                $content = "<p><em>Sorry, no items in this category.</em></p>\n";
            }
            else
            {
                $content .= "<ul>\n";
                while ($items = mysqli_fetch_array($get_items_res))
                {
                    $item_url = "items3.php?id={$items['id']}";
                    $item_title = stripslashes($items['title']);
                    $item_price = $items['price'];
                    $item_photo = $items['photo'];
                    $item_username = $items['username'];
                    $item_date = $items['date'];
                    $content .= "";
              list($width) = getimagesize($item_photo);
  // set the maximum width of the image here
  $maxWidth = 100;
  if ($width > $maxWidth);
             
             
                    $content .= "<table width=\"603\" border=\"0\"><tr><td width=\"101\"> <a href=\"{$item_url}\">
                    <img alt=\"Image\" border=0 width=\"{$maxWidth}\" src=\"{$item_photo}\" /></a><td width=\"201\">      <a href=\"{$item_url}\">{$item_title}</a></td>
                    <td width=\"109\">{$item_username}</td><td width=\"101\"> {$item_date}</td><td width=\"99\">      \${$item_price} USD </td></tr></table>";
                    $content .= "\n";
                
                }
  
                $content .= "</ul>\n";
            }
         }  
    }
}

?>

  <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Art</title>
<link href="styles/sgallery2.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a:link {
color: #09C;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #09C;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: none;
}
-->
</style></head>
<div align="center">
<body>


<div class="mainbody">
  <div class="header1">
  [color=red]<?php echo $_SESSION['username']; ?>[/color]</div>
  <div class="leftsidebar"><span class="titles">    Art Categories</span><br />
    <br />
    <?php echo $categoryList; ?>
    <br />

Link to comment
Share on other sites

In the user login form delete the line which says:

$_SESSION['username'] = $_POST['username'];

You don't need that yet.

 

Then in the checklogin.php file change the line which says:

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

session_register("username");

 

to:

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['username'] = $myusername;

This way you have set the $_SESSION['username'] once you've succesfully logged in.  You can call this $_SESSION variable in any php file in which you start a session.

Link to comment
Share on other sites

Thank you...thank you...thank you...swisse!  It worked!  I was struggling and struggling with that one for several hours yesterday until I just crashed and decided to come back to it today with a refreshed mind.  Glad I did.  Thank you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.