![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0
![]() |
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.';
}
?><?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;
}
?> |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Sep 2004
Posts: 207
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0
![]() |
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?
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2005
Posts: 44
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0
![]() |
Magic E you're a champion, it works pretty much exactly as i wanted and with a whole lot less code! Thanks alot.
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jan 2005
Posts: 44
Rep Power: 0
![]() |
npz happy to help
![]() |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jun 2005
Location: Queensland
Posts: 37
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jan 2005
Posts: 44
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|