Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 30th, 2005, 9:25 PM   #1
foxcity911
Programmer
 
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0 foxcity911 is on a distinguished road
Exclamation session variable problem

ive set the session up correctly im pretty sure, but when it goes from user_authenticate.php to members.php the members.php screen says that the username and password variables are undefined. why hasnt the session variables registered properly?
user_authenticate.php
<?

$user = $_POST['user']; 
$pass = $_POST['password']; 

//set the database connection variables 

$dbHost = "localhost"; 
$dbUser = "root";  
$dbDatabase = "contact"; 

//connet to the database 

$db = mysql_connect("$dbHost", "$dbUser") or die ("DB is fucked"); 

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database."); 

$result=mysql_query("SELECT * FROM logins WHERE user='$user' AND pass='$pass'", $db); 

//check that at least one row was returned 

$rowCheck = mysql_num_rows($result) or die ("Incorrect username or password!<P><A href='userlogin.php'>Return to Login</a>"); 
if($rowCheck >= 1){ 
while($row = mysql_fetch_array($result)){ 

  //start the session and register a variable 

  session_start(); 
  session_register("$user"); 
 session_register("$pass");

$HTTP_SESSION_VARS ["$user"] = $username;
$HTTP_SESSION_VARS ["$password"] = $password;
  

  //successful login code will go here... 
header ("Location: members.php");
 
  } 

  } 
  else { 

  //if nothing is returned by the query, unsuccessful login code goes here... 

  echo 'Incorrect login name or password. Please try again.'; 
  } 
 ?>
members.php
<?php

$dbHost = "localhost"; 
$dbUser = "root";  
$dbDatabase = "contact"; 


//connect to the database 

$db = mysql_connect("$dbHost", "$dbUser") or die ("DB is fucked"); 

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database."); 

$result=mysql_query("SELECT * FROM logins WHERE user='$username' AND pass='$password'", $db); 

//check that at least one row was returned 

$rowCheck = mysql_num_rows($result) or die ("Incorrect username or password!<P><A href='userlogin.php'>Return to Login</a>"); 
if($rowCheck <= 1){ 
while($row = mysql_fetch_array($result))

header ("Location: login.php");

}
 else {


echo ("Welcome Members");

echo "<P><A href='addform.php'>Add a contact</a>";

echo $user;
}

?>
foxcity911 is offline   Reply With Quote
Old Jun 30th, 2005, 11:41 PM   #2
BlazingWolf
Hobbyist Programmer
 
Join Date: Sep 2004
Posts: 207
Rep Power: 5 BlazingWolf is on a distinguished road
You need to use session_start() on every page that you want to access the session's varibles.

Also when your setting your sessions varibles the proper way to do it is

$_SESSION['myvarible'] = 'myvalue'

session_register() is not supposed to be used any more, and $HTTP_SESSION_VARS is just a longer version of $_SESSION.
__________________
_______________________________
BlazingWolf
BlazingWolf is offline   Reply With Quote
Old Jun 30th, 2005, 11:57 PM   #3
foxcity911
Programmer
 
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0 foxcity911 is on a distinguished road
the reason i used the session_vars and session_register because $_SESSION comes up as an undefined variable even though its supposed to be a universal variable.
foxcity911 is offline   Reply With Quote
Old Jul 1st, 2005, 3:08 AM   #4
foxcity911
Programmer
 
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0 foxcity911 is on a distinguished road
okay im using $_SESSION instead now but the variables are still not registering on members.php. im using the the session register on the page aswell. the error is still the same, and i have enabled cookies completely. why is it still wrong?
foxcity911 is offline   Reply With Quote
Old Jul 1st, 2005, 4:45 AM   #5
magic_e
Programmer
 
Join Date: Jan 2005
Posts: 44
Rep Power: 0 magic_e is on a distinguished road
maybe i've missed somthing but were in your code you check to see if the session is set?

maybe i'm wrong but why dont you try this instead



members.php
[PHP]<?php
sesion_start();
$dbHost = "localhost";
$dbUser = "root";
$dbDatabase = "contact";


//connect to the database

$db = mysql_connect("$dbHost", "$dbUser") or die ("DB is fucked");

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");


//check that at least one row was returned

if (!isset($_SESSION['user'])){

header ("Location: login.php");

}else {


echo ("Welcome Members");

echo "<P><A href='addform.php'>Add a contact</a>";

echo $_SESSION['user'];
}

?>[/PHP]

user_authenticate.php
[PHP]<?
session_start();
$user = $_POST['user'];
$pass = $_POST['password'];

//set the database connection variables

$dbHost = "localhost";
$dbUser = "root";
$dbDatabase = "contact";

//connet to the database

$db = mysql_connect("$dbHost", "$dbUser") or die ("DB is fucked");

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

$result=mysql_query("SELECT * FROM logins WHERE user='$user' AND pass='$pass'", $db);

//check that at least one row was returned

$rowCheck = mysql_num_rows($result) or die ("Incorrect username or password!<P><A href='userlogin.php'>Return to Login</a>");
if($rowCheck >= 1){
while($row = mysql_fetch_array($result)){

//start the session and register a variable


$_SESSION["user"] = $user;
$_SESSION["password"] = $pass;


//successful login code will go here...
header ("Location: members.php");

}

}
else {

//if nothing is returned by the query, unsuccessful login code goes here...

echo 'Incorrect login name or password. Please try again.';
}
?>[/PHP]

hope it helps
magic_e is offline   Reply With Quote
Old Jul 1st, 2005, 5:02 AM   #6
foxcity911
Programmer
 
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0 foxcity911 is on a distinguished road
Magic E you're a champion, it works pretty much exactly as i wanted and with a whole lot less code! Thanks alot.
foxcity911 is offline   Reply With Quote
Old Jul 1st, 2005, 5:05 AM   #7
magic_e
Programmer
 
Join Date: Jan 2005
Posts: 44
Rep Power: 0 magic_e is on a distinguished road
npz happy to help
magic_e is offline   Reply With Quote
Old Jul 1st, 2005, 6:12 AM   #8
foxcity911
Programmer
 
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0 foxcity911 is on a distinguished road
another problem, good lord i feel like a moron, lol.
this page prints the contacts that have been inputted into the database depending on user.
<?php
session_start();

if (!isset($_SESSION['user'])){

header ("Location: userlogin.php");

}else {

$user=$_SESSION['user'];

$dbHost = "localhost"; 
$dbUser = "root";  
$dbDatabase = "contact"; 

//connet to the database 

$db = mysql_connect("$dbHost", "$dbUser") or die ("DB is fucked"); 

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

$result = mysql_query("SELECT * FROM contactinfo WHERE username='$user'", $db) or die ("getting info from contactinfo sucks"); 

mysql_close();

echo $result;

}

?>

it comes up resource ID#3 instead of the table info, why is this?
foxcity911 is offline   Reply With Quote
Old Jul 1st, 2005, 9:10 AM   #9
magic_e
Programmer
 
Join Date: Jan 2005
Posts: 44
Rep Power: 0 magic_e is on a distinguished road
you need to put it into a loop


[PHP]

<?php
session_start();

if (!isset($_SESSION['user'])){

header ("Location: userlogin.php");

}else {

$user= $_SESSION['user'];

$dbHost = "localhost";
$dbUser = "root";
$dbDatabase = "contact";

//connet to the database

$db = mysql_connect("$dbHost", "$dbUser") or die ("DB is fucked");

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

$res = mysql_query("SELECT * FROM contactinfo WHERE username='$user'", $db) or die ("getting info from contactinfo sucks");

//You see how it will get every single row with given from that query and then get each row
while (@$row = mysql_fetch_array($res, MYSQL_ASSOC)) {

$name = $row['name'];
}

echo $name;

mysql_close();



}

?>
[/PHP]

something like that, you will ned to change name to the title of a row in your database

then you will be able to build your page that way

hope it helps
magic_e is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC