Jump to content

USING sessions


p_tru

Recommended Posts

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:

[code]
<?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>';
}

?>[/code]

[b]and how can i use the username for this session and use it in a sql query[/b]

Help!

Petey
Link to comment
Share on other sites

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 --

[code]<?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>");
}
    
?>[/code]

If it doesn't, you don't have session capability.
Link to comment
Share on other sites

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

[code]

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


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

HELP!

Petey
Link to comment
Share on other sites

[!--quoteo(post=353599:date=Mar 10 2006, 12:33 PM:name=joecooper)--][div class=\'quotetop\']QUOTE(joecooper @ Mar 10 2006, 12:33 PM) [snapback]353599[/snapback][/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
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.