cookies/sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robojob
Forum Newbie
Posts: 6
Joined: Sun Dec 11, 2005 3:48 pm

cookies/sessions

Post by robojob »

i have this code which sets a session upon login:

Code: Select all

<?php
session_start();

$dbHost = "***"; 
$dbUser = "****"; 
$dbPass = "**"; 
$dbname = "***"; 
$username = $_POST['username'];
$password = $_POST['password'];

$db = mysql_connect($dbHost,$dbUser,$dbPass); 
mysql_select_db($dbname,$db);                 

$query = "SELECT user, pass FROM rwdlogin WHERE user = '$username'
AND pass = '$password'";
$result = mysql_query($query, $db);
if(mysql_num_rows($result)) {
   $_SESSION['loggedin'] = 1;
   header('Location: index.php');
   exit(); }
else {
   header('Location: login.php');
   exit(); }
?>
how can i add to this a second session/cookie that stores the username so that i can do further queries on other members pages related to that user...?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$result = mysql_query($query, $db);
if(mysql_num_rows($result)) {
   //fetch results
   $row = mysql_fetch_assoc($result);
   $_SESSION['loggedin'] = 1;
   //assign 'user' to session
   $_SESSION['user'] = $row['user'];
   header('Location: index.php');
   exit(); 
}
robojob
Forum Newbie
Posts: 6
Joined: Sun Dec 11, 2005 3:48 pm

Post by robojob »

and how would i then recieve the session on any page and for example use it in an sql query?
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

i think you need to read the manual about $_SESSION

in other page you just need to access $_SESSION['user'] but anyway if you use a COOKIE will be more simple maybe...

search in manual for function setcookie
robojob
Forum Newbie
Posts: 6
Joined: Sun Dec 11, 2005 3:48 pm

Post by robojob »

ok so i am setting the session with the code that jcart supplied and in theory to display the value in the session i should be able to use this?

Code: Select all

<?php 
session_start(); 
echo " Welcome " $user; 
?>
However this gives me the following error:

Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in blah/blah/blah/test.php on line 3

HELP!! :)
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

sometimes you got your code problems resolved just by reading the manual...

http://www.php.net/manual/en/features.sessions.php

anyway in that case could be

Code: Select all

session_start();
if(isset($_SESSION['user'])) { 
      echo $_SESSION['user']; 
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

robojob wrote:

Code: Select all

<?php 
session_start(); 
echo " Welcome " $user; 
?>
...
Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in blah/blah/blah/test.php on line 3
I would also recommend you reading the manual on strings

This is how you would do it using double quotes

Code: Select all

echo "Welcome $user";
If your using single quotes, then you'll have to escape the string if you want to parse a variable

Code: Select all

echo 'Welcome '.$user;
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

robojob wrote:ok so i am setting the session with the code that jcart supplied and in theory to display the value in the session i should be able to use this?

Code: Select all

<?php 
session_start(); 
echo " Welcome " $user; 
?>
However this gives me the following error:

Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in blah/blah/blah/test.php on line 3

HELP!! :)
are you using global variables...then stop using it...access through $_SESSION["user"]
robojob
Forum Newbie
Posts: 6
Joined: Sun Dec 11, 2005 3:48 pm

Post by robojob »

i know i should use the manual but i find it more confusing than simple help that you can get here, the below works, but how can i use that in an sql query, for example, where user = the session value...
duk wrote:sometimes you got your code problems resolved just by reading the manual...

http://www.php.net/manual/en/features.sessions.php

anyway in that case could be

Code: Select all

session_start();
if(isset($_SESSION['user'])) { 
      echo $_SESSION['user']; 
}
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Right an sql statement similart to what's been posted.

Code: Select all

$user = $_SESSION['user'];
$sql = "SELECT * FROM users WHERE user='$user'";
Something like that will work.
robojob
Forum Newbie
Posts: 6
Joined: Sun Dec 11, 2005 3:48 pm

Post by robojob »

k i tried this:

Code: Select all

<? 

mysql_connect("***","***","****"); 
	

mysql_select_db("blahblah"); 


if(!isset($cmd)) 
{
  
   $user = $_SESSION['user'];
   $result = mysql_query("SELECT * FROM users WHERE user='$user'"); 
   
   
   while($r=mysql_fetch_array($result)) 
   { 
      
      $user=$r["user"];
      $pass=$r["pass"];
     
	
      echo "<li>$user - $pass";
      echo "<br>";
    }
}
?>
and get this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

The $user global variable you are using might not contain any value and when you tried a select on it, it should have returned no results.

Two steps to counter it...
1. before doing a select operation, make sure inputs are valid,,,i.e user here
2. After select has been run, check for validity of result...simply if (!$result) display error;
Post Reply