database login issue

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
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

database login issue

Post by dyonak »

Ok my database login was working until I wanted to make a generic template using PHP includes for everything static on my page. Now it seems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

<?php 
if(empty($_SESSION['user_name'])) {
             die('<div id="login"><form name="login" 

method="post" action="validate.php">
username:<input type="text" name="user_name"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try <a href="login.php">logging in</a> again or contact the <a href="mailto:admin@example.com">system administrator</a></div>');
}
else
{
	print "Welcome $user_name";
}
?>
and here's my validate.php

Code: Select all

&lt;?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the &quote;login&quote; database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = &quote;SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'&quote;;
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_r everything static on my page. Now it seems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

<?php 
if(empty($_SESSION['user_name'])) {
             die('&lt;div id=&quote;login&quote;&gt;&lt;form name=&quote;login&quote; 

method=&quote;post&quote; action=&quote;validate.php&quote;&gt;
username:&lt;input type=&quote;text&quote; name=&quote;user_name&quote;&gt;&lt;br&gt;
password:&lt;input type=&quote;password&quote; name=&quote;password&quote;&gt;&lt;br&gt;
&lt;input type=&quote;submit&quote; value=&quote;submit&quote;&gt;
&lt;/form&gt;An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try &lt;a href=&quote;login.php&quote;&gt;logging in&lt;/a&gt; again or contact the &lt;a href=&quote;mailto:admin@example.com&quote;&gt;system administrator&lt;/a&gt;&lt;/div&gt;');
}
else
{
	print &quote;Welcome $user_name&quote;;
}
?&gt;
and here's my validate.php

Code: Select all

&lt;?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the &quote;login&quote; database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = &quote;SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'&quote;;
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);
	$_SESSION&#1111;'affected_rows'] = $affected_rows;
	//if there's exactly one result, the user is validated. Otherwise, he's invalid
if($affected_rows == 1) {
		print 'validated';
		//add the user to our session variables
		$_SESSION&#1111;'user_name'] og back in.

Here's my footer.php:

Code: Select all

<?php 
if(empty($_SESSION['user_name'])) {
             die('<div id=&quote;login&quote;&gt;&lt;form name=&quote;login&quote; 

method=&quote;post&quote; action=&quote;validate.php&quote;&gt;
username:&lt;input type=&quote;text&quote; name=&quote;user_name&quote;&gt;&lt;br&gt;
password:&lt;input type=&quote;password&quote; name=&quote;password&quote;&gt;&lt;br&gt;
&lt;input type=&quote;submit&quote; value=&quote;submit&quote;&gt;
&lt;/form&gt;An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try &lt;a href=&quote;login.php&quote;&gt;logging in&lt;/a&gt; again or contact the &lt;a href=&quote;mailto:admin@example.com&quote;&gt;system administrator&lt;/a&gt;&lt;/div&gt;');
}
else
{
	print &quote;Welcome $user_name&quote;;
}
?&gt;
and here's my validate.php

Code: Select all

&lt;?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the &quote;login&quote; database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = &quote;SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'&quote;;
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);
	$_SESSION&#1111;'affected_rows'] = $affected_rows;
	//if there's exseems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

&lt;?php 
if(empty($_SESSION['user_name'])) {
             die('<div id="login&quote;&gt;&lt;form name=&quote;login&quote; 

method=&quote;post&quote; action=&quote;validate.php&quote;&gt;
username:&lt;input type=&quote;text&quote; name=&quote;user_name&quote;&gt;&lt;br&gt;
password:&lt;input type=&quote;password&quote; name=&quote;password&quote;&gt;&lt;br&gt;
&lt;input type=&quote;submit&quote; value=&quote;submit&quote;&gt;
&lt;/form&gt;An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try &lt;a href=&quote;login.php&quote;&gt;logging in&lt;/a&gt; again or contact the &lt;a href=&quote;mailto:admin@example.com&quote;&gt;system administrator&lt;/a&gt;&lt;/div&gt;');
}
else
{
	print &quote;Welcome $user_name&quote;;
}
?&gt;
and here's my validate.php

Code: Select all

&lt;?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the &quote;login&quote; database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = &quote;SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'&quote;;
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affecte a generic template using PHP includes for everything static on my page. Now it seems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

&lt;?php 
if(empty($_SESSION&#1111;'user_name'])) {
             die('&lt;div id=&quote;login&quote;&gt;&lt;form name=&quote;login&quote; 

method=&quote;post&quote; action=&quote;validate.php&quote;&gt;
username:&lt;input type=&quote;text&quote; name=&quote;user_name&quote;&gt;&lt;br&gt;
password:&lt;input type=&quote;password&quote; name=&quote;password&quote;&gt;&lt;br&gt;
&lt;input type=&quote;submit&quote; value=&quote;submit&quote;&gt;
&lt;/form&gt;An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try &lt;a href=&quote;login.php&quote;&gt;logging in&lt;/a&gt; again or contact the &lt;a href=&quote;mailto:admin@example.com&quote;&gt;system administrator&lt;/a&gt;&lt;/div&gt;');
}
else
{
	print &quote;Welcome $user_name&quote;;
}
?&gt;
and here's my validate.php

Code: Select all

&lt;?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the &quote;login&quote; database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = &quote;SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'&quote;;
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);
	$_SESSION&#1111;'affected_rows'] = $affected_rows;
	//if there's exacin was working until I wanted to make a generic template using PHP includes for everything static on my page. Now it seems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

&lt;?php 
if(empty($_SESSION&#1111;'user_name'])) {
             die('&lt;div id=&quote;login&quote;&gt;&lt;form name=&quote;login&quote; 

method=&quote;post&quote; action=&quote;validate.php&quote;&gt;
username:&lt;input type=&quote;text&quote; name=&quote;user_name&quote;&gt;&lt;br&gt;
password:&lt;input type=&quote;password&quote; name=&quote;password&quote;&gt;&lt;br&gt;
&lt;input type=&quote;submit&quote; value=&quote;submit&quote;&gt;
&lt;/form&gt;An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try &lt;a href=&quote;login.php&quote;&gt;logging in&lt;/a&gt; again or contact the &lt;a href=&quote;mailto:admin@example.com&quote;&gt;system administrator&lt;/a&gt;&lt;/div&gt;');
}
else
{
	print &quote;Welcome $user_name&quote;;
}
?&gt;
and here's my validate.php

Code: Select all

&lt;?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the &quote;login&quote; database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = &quote;SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'&quote;;
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);
	$_SESSION&#1111;'affected_rows'] = $affected_rows;
	//if there's exactly one result, the user is validated. Oin was working until I wanted to make a generic template using PHP includes for everything static on my page. Now it seems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

&lt;?php 
if(empty($_SESSION&#1111;'user_name'])) {
             die('&lt;div id=&quote;login&quote;&gt;&lt;form name=&quote;login&quote; 

method=&quote;post&quote; action=&quote;validate.php&quote;&gt;
username:&lt;input type=&quote;text&quote; name=&quote;user_name&quote;&gt;&lt;br&gt;
password:&lt;input type=&quote;password&quote; name=&quote;password&quote;&gt;&lt;br&gt;
&lt;input type=&quote;submit&quote; value=&quote;submit&quote;&gt;
&lt;/form&gt;An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try &lt;a href=&quote;login.php&quote;&gt;logging in&lt;/a&gt; again or contact the &lt;a href=&quote;mailto:admin@example.com&quote;&gt;system administrator&lt;/a&gt;&lt;/div&gt;');
}
else
{
	print &quote;Welcome $user_name&quote;;
}
?&gt;
and here's my validate.php

Code: Select all

&lt;?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the &quote;login&quote; database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = &quote;SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'&quote;;
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);
	$_SESSION&#1111;'affected_rows'] = $affected_rows;
	//if there's exactly one result, the user is validated. Otherwise, he's invalid
if($affected_rows == 1) {
		print 'static on my page. Now it seems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

<?php 
if(empty($_SESSION['user_name'])) {
             die('<div id="login"><form name="login" 

method="post" action="validate.php">
username:<input type="text" name="user_name"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try <a href="login.php">logging in</a> again or contact the <a href="mailto:admin@example.com">system administrator</a></div>');
}
else
{
	print "Welcome $user_name";
}
?>
andeems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

<?php 
if(empty($_SESSION['user_name'])) {
             die('<div id="login"><form name="login" 

method="post" action="validate.php">
username:<input type="text" name="user_name"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try <a href="login.php">logging in</a> again or contact the <a href="mailto:admin@example.com">system administrator</a></div>');
}
else
{
	print "Welcome $user_name";
}
?>
and here's my validate.php

Code: Select all

&lt;?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the &quote;login&quote; database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = &quote;SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'&quote;;
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$il I wanted to make a generic template using PHP includes for everything static on my page. Now it seems that the session variables aren't registering, it shows me logged in when I'm at the validation page but once I go back to the index it's just asking me to log back in.

Here's my footer.php:

Code: Select all

&lt;?php 
if(empty($_SESSION&#1111;'user_name'])) {
             die('<div id="login">&lt;form name=&quote;login&quote; 

method=&quote;post&quote; action="validate.php">
username:<input type="text" name="user_name">&lt;br&gt;
password:&lt;input type=&quote;password&quote; name=&quote;password&quote;&gt;&lt;br&gt;
&lt;input type=&quote;submit&quote; value=&quote;submit&quote;&gt;
&lt;/form&gt;An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try &lt;a href=&quote;login.php&quote;&gt;logging in&lt;/a&gt; again or contact the &lt;a href=&quote;mailto:admin@example.com&quote;&gt;system administrator&lt;/a&gt;&lt;/div&gt;');
}
else
{
	print &quote;Welcome $user_name&quote;;
}
?&gt;
and here's my validate.php

Code: Select all

<?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST['user_name'];
	$password = $_POST['password'];

	//connect to the DB and select the "login" database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = "SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'";
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($resul
<?php 
if(empty($_SESSION['user_name'])) {
             die('<div id="login"><form name="login" 

method="post" action="validate.php">
username:<input type="text" name="user_name"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>An error has ocurred. It may be that you have not 
logged in, or that your session has expired. Please try <a href="login.php">logging in</a> again or contact the <a href="mailto:admin@example.com">system administrator</a></div>');
}
else
{
	print "Welcome $user_name";
}
?>
and here's my validate.php

Code: Select all

<?php
	$db_user = 'mysql_username';
	$db_pass = 'mysql_password';
	$user_name = $_POST['user_name'];
	$password = $_POST['password'];

	//connect to the DB and select the "login" database
	$connection = mysql_connect('host','user','pass') or die(mysql_error());
	mysql_select_db('database', $connection) or die(mysql_error());

	//set up the query
	$query = "SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'";
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);
	$_SESSION['affected_rows'] = $affected_rows;
	//if there's exactly one result, the user is validated. Otherwise, he's invalid
if($affected_rows == 1) {
		print 'validated';
		//add the user to our session variables
		$_SESSION['user_name'] = $user_name;
	}
	else {
		print 'not valid';
	}
?>
Do you guys see anything wrong here?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

looks to be in order it does. Using different session names are you? Made sure you have session_start() on your index page have you?
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

Yes it's referenced in my header:

Code: Select all

&lt;?php
	session_start;
	header("Cache-control: private");
	print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Conteder("Cache-control: private");
	print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quote;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">&lt;head&gt;&lt;meta http-equiv=&quote;Content-Type&quote; content=&quote;text/html; charset=iso-8859-1&quote; /&gt;';
?&gt;
The site is [url=htt1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>&ammp;quote;);
print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- ... &quote;&gt;
&lt;html xmlns=&quote;http://www.w3.org/1999/xhtml"><head><meta http-
<?php
session_start;
header("Cache-control: private");
print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- ... al.dtd"&gt;
&lt;html xmlns=&quote;http://www.w3.org/1999/xhtml&quote;&gt;&lt;head&gt;&d in my header:

Code: Select all

<?php
	session_start;
	header("Cache-control: private");
	print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quote;&gt;
&lt;html xmlns=&quote;http://www.w3.org/1999/xhtml&quote;&gt;&lt;head&gt;&lt;meta http-equiv=&quote;Content-Tyd in my header:

Code: Select all

<?php
	session_start;
	header("Cache-control: private");
	print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859e;Cache-control: private");
	print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';
?>
The site is hereif you want to take a look at what I'm talking about. Use the username test and password test.
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

Found the problem I did. Forgetten the parantheses after session_start I had. Learning from my mistakes I am.
Post Reply