Jump to content


Photo

USING sessions


  • Please log in to reply
4 replies to this topic

#1 p_tru

p_tru
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 10 March 2006 - 03:37 AM

I am trying to use sessions on one of my pages but the session does not seem to be working, can someone tell me if i have created the session right:

<?php session_start(); ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<?php
///connect to the database
$conn = @mysql_connect("mysql.cms.gre.ac.uk","tp311",password);
@mysql_select_db ("mdb_tp311", $conn);
?>
<html>

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="pete.css" />
</head>

<body>
<?php 
if (isset($_POST['login'])) {
  $message = NULL;
/*
the following code checks whether anything has
been entered into the two text boxes
*/
 if (empty($_POST['user'])) {
$user = FALSE;
$message .= '<p>whats your username</p>';
} else { 
$user = $_POST['user'];
}


if (empty($_POST['Pass'])){
 $Pass = FALSE;
  $message .='<p>password</p>';
   } else {
  $Pass = $_POST['Pass'];
   }
             
if ($user && $Pass){
$query = "SELECT username FROM customer WHERE username='$user' AND password ='$Pass' ";
$result = @mysql_query ($query,$conn);
                          
if ($result){
$row = mysql_fetch_array ($result, MYSQL_NUM);
                

//mysql_fetch array fetches a result row as an assosiative array, numeric or both
                           
if ($row){/*a*/
$_SESSION['username'] = $row[0];
                
echo '<p>match found: you are now logged in</p><br>
<p>Please Click <a href="User.php"><u>here</u></a></p>';
$_SESSION[validated] = "yes";
     }/*a*/
exit();
                
/*display wrong password or username*/    
} else {
       $message .= 'no match found';
   }
   }

}
?>
<table class="main">
<tr><td colspan="2"><img src= ""/></td><td>
<?php 

if (isset( $_SESSION['username'] )) {
echo '<p><a href="logout.php">Logout</a></p><br/>';
}else{

echo "<form action='" . $_SERVER['PHP_SHELF'] . "' method='POST' />";
echo "<div align='right'>";
echo "Username:<input type='text' name='user' value='";

if(isset($_POST['user'])) {
  echo $_POST['user'];
}

echo "' ><br>";
echo "Password:<input type='password' name='Pass' value='";

if(isset($_POST['Pass'])) {
  echo $_POST['Pass'];
}

echo "' ><br>";
echo "<input type='submit' name='login' value='login'></div>";
echo "</form>";
echo '<br><p><a href="reg.php">Register</a></p></td><td></td></tr>';
}

?>

and how can i use the username for this session and use it in a sql query

Help!

Petey

#2 k.soule

k.soule
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationIllinois

Posted 10 March 2006 - 03:49 AM

Number one: Session capability must be enabled on your server, I don't think it is by default if you are hosting your own server.

Number two: session_start() must come before ANY other output. Try this code to see if sessions work in their purest form --

<?php

session_start();

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

if($_GET['action'] === 'echo') {
    print $_SESSION['username'] . " -- Sessions work!";
}
else {
    print("<a href='" . $_SERVER['PHP_SELF'] . "?action=echo'>
           Click this link to test sessions
           </a>");
}
    
?>

If it doesn't, you don't have session capability.

#3 p_tru

p_tru
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 10 March 2006 - 12:32 PM

Yes i have session capability, this is the other page i am trying to use the session:


<?php 
session_start();

include('head.inc'); 
?>
<?php

$_SESSION['username'] = $user;

echo '<table><tr>';

$list ="SELECT i.title, i.cost, i.picture
FROM item i, Recommended_item r, p_history p, customer c
WHERE r.item1_id = p.ItemNo
AND r.item2_id = i.item_id
AND p.customer_id = c.customer_id
AND c.username = '$user'";
$list_result = @mysql_query($list,$conn);
while ($row= mysql_fetch_array($list_result, MYSQL_ASSOC)){

echo'<td><img class="image" width =60px height =100px src="' .$row["picture"].  '" alt="picture"/><br> 
<a href="User.php?id=' .$row['Title'] . '">' .$row['cost'].  '</a><br>' .$row['cost']. '<br><h1>Recomended Items</h1></td></tr><tr>';
}
echo '</tr></table>';
?>

<?php include('foot.inc');?>


Is this right to use with my other code where i created the session???

HELP!

Petey

#4 joecooper

joecooper
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 10 March 2006 - 12:33 PM

$_SESSION['username'] = $user;

that will make the session username to be what ever $user is on that page.

are you sure youve got this the wrong way around?
Signature:
[/a]
[a href="http://www.planet-so...=1999&lngWId=8" target="_blank"]EzLogin 1.0[/a]
[a href="http://www.essexracers.com" target="_blank"]Essexracers.com[/a]
Msn Messenger: joe@joeyjoe.co.uk

#5 p_tru

p_tru
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 10 March 2006 - 08:24 PM

[!--quoteo(post=353599:date=Mar 10 2006, 12:33 PM:name=joecooper)--][div class=\'quotetop\']QUOTE(joecooper @ Mar 10 2006, 12:33 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$_SESSION['username'] = $user;

that will make the session username to be what ever $user is on that page.

are you sure youve got this the wrong way around?
[/quote]



I try it the other way and it seems to work now, so thanks

Petey




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users