Session problem, please help me!!!

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
askme
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2004 3:17 am

Session problem, please help me!!!

Post by askme »

Here is the simple user authentication I have made and try to verify the name and password from MySql database. The problem is that I cannot pass the session variable between pages, I mean to SUCCESS.PHP page.
All pages codes are given below, It has 3 pages,
1. LOGIN.PHP
2. AUTHENTICATE.PHP
3. SUCCESS.PHP

Here are the codes:

LOGIN.PHP

Code: Select all

<html>

<head>
<title>login Page</title>
</head>
<body>

<form method="POST" action="authenticate.php">
  
<p>Name:<input type="text" name="name" size="20"></p>
<p>Password:<input type="password" name="password" size="20"></p>
<p><input type="submit" value="Submit"><input type="reset" value="Reset"></p>

</form>
</body>
</html>
AUTHENTICATE.PHP

Code: Select all

<?php 
session_start(); 
$name = $HTTP_POST_VARS&#1111;'name'];
$password = $HTTP_POST_VARS&#1111;'password'];

$HTTP_SESSION_VARS&#1111;'name']=$name;
$HTTP_SESSION_VARS&#1111;'password']=$password;


  //connect to the database... 
 
 mysql_connect(HOST, USER, PASS); 
 mysql_select_db(DB); 
 
 //...and get number of result matched 
 
$result = mysql_query("SELECT from login WHERE name='&#123;$HTTP_POST_VARS&#1111;'name']&#125;' AND password='&#123;$HTTP_POST_VARS&#1111;'password']&#125;'"); 

$rows=mysql_mun_rows($result)

if ($rows > 0)
&#123;

//Redirect the user to success page to prompt him 

session_register('$name');  
require('success.php');
exit; 

       &#125; 
else
     &#123;

//unsuccessful login 

echo 'sorry, you are not allowed, try again';
require('login.php');
exit;
 
    &#125; 
?>

SUCCESS.PHP

Code: Select all

<?php 

session_start(); 
echo 'Welcome to Autherized Area';
echo 'you are logged as';
echo $HTTP_SESSION_VARS&#1111;'name'];
  
?>
Please let me know, What is the problem, I could not get the session variable at SUCCESS.PHP page.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

First question - what version of PHP are you using?

Mac
askme
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2004 3:17 am

Post by askme »

I am using PHP 4.0.1 on Windows XP. I have Installed from PHP triad for Windows!!!
Last edited by askme on Fri Feb 06, 2004 3:47 am, edited 1 time in total.
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

This may seem trivial, but you forgot the semicolon at the end of the sql statement $rows=mysql_mun_rows($result)

Sometimes, and Im not sure if it matters in this case or not so dont sue me, statements will leak by making a person think everything is working but they are not. Especially if you have any parse error settings off...etc..
The reason I think it could be a problem, is because you use it for a comparison in your "if" statement. In reality, it could be returning nothing and simply exiting out without properly saving the session register.
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

What I should of done was post a example of what I mean.

I always use the empty function because having a set number can sometimes cause problems with SQL statements, but..if it is empty, she should just quit rite there.

Code: Select all

if (empty($rows )) &#123;
   echo 'There was nothing returned from the DB';
   exit;
&#125;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

4.0.1 are you sure? If so you should really upgrade to 4.3 as 4.0 is now very outdated and will make it difficult for you to manage sessions.

Mac
askme
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2004 3:17 am

Post by askme »

Great Advice from you people, I will try to upgrade my PHP and then try the code again, by the way where can I get PHP Triad Upgrade!!!

(Because it is very easy to Install, Apache, MySql, and PHP together, all it configure by itself.)
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

maybe a couple thigns here:

mysql_mun_rows() ? try mysql_num_rows()

and in your SELECT query, add a * between SELECT and FROM
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

also noticed that
session_register("$name") notation should be used
session_register("name")
write only the name of variable without '$' sign
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

Illusionist wrote:maybe a couple thigns here:

mysql_mun_rows() ? try mysql_num_rows()

and in your SELECT query, add a * between SELECT and FROM

damn man, good eye!!
That one slipped right through the cracks.

Wow...its funny how a little tiny mistake in spelling can be so difficult to find sometimes, and I am sure that with that problem solved, coupled with some of the other things mentioned in this thread, he will be back up in running in no time.
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

Lol...what is funnier yet, I copy/pasted that sucker as is in my above post regarding the semi-colon.
askme
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2004 3:17 am

Post by askme »

It was typing mistakes,

Code: Select all

mysql_mun_rows() --------->mysql_num_rows() 

and 

 SELECT  * FROM login WHERE ...........
But the problem is still I cannot get my problem solved, cannot pass session variable to other pages.

By the way there is any equavalent code in PHP for this ASP code

Code: Select all

Response.redirect('login.asp');
no1shah
Forum Newbie
Posts: 16
Joined: Tue Feb 10, 2004 2:20 am
Contact:

Post by no1shah »

HEllo Friend
Two days earlier i was also facing the same problem,
First is u can then upgrade ur php version sither 4.3.4/4.3.3

Next check the php.ini file and see whether register_global is on or off.
By default it should be off and i reccomend u to use it as off,in version 4.3.4 create the session using $_SESSION and use $_POST.
r u still getting prob.sssssssssss
mahara
Forum Commoner
Posts: 37
Joined: Wed Nov 13, 2002 1:08 am
Location: Bandung, Jawa Barat, Indonesia

Post by mahara »

askme wrote:Great Advice from you people, I will try to upgrade my PHP and then try the code again, by the way where can I get PHP Triad Upgrade!!!

(Because it is very easy to Install, Apache, MySql, and PHP together, all it configure by itself.)
So, what is the version of PHP you are using now?
weecol
Forum Newbie
Posts: 1
Joined: Thu Feb 12, 2004 3:44 am

redirects in php

Post by weecol »

regarding the query on how to do redirect headers in php
try calling header with location("location: <URL>");
according to php.net it handles the 3xx header as well but you can set it using header("Status: 3xx <approreate string>"); 8)

When I need a solution I will first look up php.net

RTFM
Post Reply