Jump to content

[SOLVED] session variable not printing


bhavin_85

Recommended Posts

hi guys

 

just found this forum  :) looks like its got loadsa usefull stuff on it which is be reallly helpful when makin my site....my first question

 

ive created a login form and a welcom page but i cant seem to get the session variables to print on the second page. Im not great at coding so ive hada go but it doesnt seem to work...can any1 give me some points?

 

login page:

<?
session_start();
$first_name = $_SESSION['first_name'];
if(isset($_GET['reg'])){
$reg=$_GET['reg'];
}else{
$reg="";
}
//if($reg==1){
//$msg1="<font color=\"#FF0000\"><b>Your details have been added, please login</b></font>";
//}elseif($reg==2){
//$msg1="<font color=\"#FF0000\"><b>You have been successfully logged out.</b></font>";
//}

if(isset($_POST['submit'])){
if( empty($_POST['uname']) && (empty($_POST['upass']))){
header( "Location:Messages.php?msg=1" ); 
exit();
}
//transfer to shorter var
$n=$_POST['uname'];	
$p=$_POST['pwd'];

include('config.php');

$sql="SELECT * FROM customer WHERE uname='$n' AND pwd=MD5('$p')"; 
$query=mysql_query($sql) or die("Queryfailed:".mysql_error());
if (mysql_num_rows($query) != 0) { 

$mytime=time();
$mytime=date("H:i:s A",$mytime);
$_SESSION['time'] = $mytime;
$_SESSION['status'] = 'logged';
$_SESSION['username'] = $n;

header("location:welcome.php");
exit;
} else {
$_SESSION['status'] = 'not logged';
header("Location:Messages.php?msg=2" ); 
exit();
}
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>P.B.L. Jewellers Ltd.</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#000000" text="#92894B">
<table width=762 height=685 border=0 align="center" cellpadding=0 cellspacing=0 id="Table_01">
<tr>

    <td colspan=3> <img src="images/img_main_page.gif" width=762 height=48 alt=""></TD>
</tr>
<tr>

    <td rowspan=2> <img src="images/img_main_page-02.png" width=319 height=257 alt="top"></TD>
	<td><table width="100%" border="0">
              <tr>
                <td height="59"> </td>
                <td>Customer Login</td>
                 <form name="form1" method="post" action=<? echo $_SERVER['PHP_SELF'];?>>
			 <td><label>
                <input name="uname" type="text" id="uname" value="username" size="18">
                </label></td>
                <td><input name="pwd" type="password" id="pwd" value="password" size="18"></td>
                <td><label>
                  <input name="submit" type="submit" value="Login"></form>
                </label></td>
              </tr>
            </table>
		</td>

    <td rowspan=2> <img src="images/img_main_page-04.gif" width=41 height=257 alt=""></td>
</tr>
<tr>

    <td valign="top"><p><br>
        Welcome to P.B.L Jewellers Online</p>
      <p>Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy 
        Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text 
        Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy 
        Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text 
        Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy Text Dummy 
        Text Dummy Text Dummy Text</p></td>
</tr>
<tr>

    <td> <img src="images/img_main_page-06.gif" width=319 height=355 alt=""></td>

    <td>Pictures of products to go here</td>

    <td> <img src="images/img_main_page-08.gif" width=41 height=355 alt=""></td>
</tr>
<tr>

    <td colspan=3> <img src="images/img_main_page-09.gif" width=762 height=25 alt=""></TD>
</tr>
</table>
</body>
</html>

 

as you can see above there is a $first name...the firstname is stored in the table which is associated with that login id.

 

welcome page (page 2)

<?
session_start();
if ( empty($_SESSION['username'])){
header("location:default.php");
exit;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK REL=StyleSheet HREF="stylelog.css">
</head>

<body>
<div align="center">
  <div id="Centre">
  <? echo $_SESSION['username'];?>,WELCOME TO MY PAGE<br>
  You have passed all security checks!<br>
  <?
  echo "You've been logged in since" . $_SESSION['time'] . "<br>";
  echo "your Login id" . $_SESSION['first_name'] . "<br>";
  echo '<a href="logout.php">Click here to logout</a>';
  echo "<br>";
  echo '<a href="' .$_SERVER['PHP_SELF']. '?action=change"> Change Password</a>';
  ?>
  <?
  include "fns.php";
  $action=$_GET['action'];
  if($action=="change"){
  changepw($_SESSION['username']);
  update($newpass,$_SESSION['username']);
  }else{
  
  }
    ?>
</div>
</body>
</html>

 

ive tried to print the code using echo then the variable name....what am i doing wrong?

 

thanks 4 ur help :)

Link to comment
Share on other sites

There's a lot of code to look through for a vague problem.

On your second page, do print_r($_SESSION) and you will see all the session vars.

In your first script you do

$first_name = $_SESSION['first_name'];

 

But I never see you assign it INTO the session.

Link to comment
Share on other sites

yep i tihnk thats the problem, how do i assign it to the session? bearing inmind that it isinformation which is in the same table as the login information so in effect i need the page to recognise the login id, then get the other information in that same table row...and print it on the second page

Link to comment
Share on other sites

You did it down here:

$mytime=time();

$mytime=date("H:i:s A",$mytime);

$_SESSION['time'] = $mytime;

$_SESSION['status'] = 'logged';

$_SESSION['username'] = $n;

 

yea but those are not details from the same row in the table that has the login id...if you look at my previous post i explain what i mean...sorry u replied while i was editing my post  :) thanks 4 the uber speedy replied though :)

Link to comment
Share on other sites

ok ive had a look on php.net and im not sure what i should be lookin for....how do i call the fields from the database?  ??? do i need to run a query that returns all of the fields then use that? or does sessions give me the ability to do that?

 

sorry for askin the basic questions but im a total n00bie to php

Link to comment
Share on other sites

ive had a read of that stuff....am i right in sayin that to get the information i have to write an sql query that will get all of the fields from the database. Ok so now i only need to figure one thing out...i need to transfer my customer_id from the first page to the second so i can run the sql query on the second page by matching the customer id signed in with to the customer id in the table...so how would i bring the id from the first pag to the next?

Link to comment
Share on other sites

cheers 4 that :)

 

had to tweak it abit so that it worked with my code

$sql="SELECT * FROM customer WHERE uname='$n' AND pwd=MD5('$p')"; 
$query=mysql_query($sql) or die("Queryfailed:".mysql_error());
if (mysql_num_rows($query) != 0) { 
$row = mysql_fetch_assoc($query);
$_SESSION['first_name'] = $row['first_name'];

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.