Programming Tools
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingProgramming Tools

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 9th, 2003, 11:40 PM
Vantera Vantera is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: South Coast of NSW, Australia
Posts: 108 Vantera User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Vantera
Article Discussion: PHP for Beginners by a Beginner: Simple Login, Logout, and Sessio

If you have any questions or comments about this article please post them here.

This forum post relates to this article
__________________
Kind Regards,
John Rebbeck
john@interspire.com
ICQ# 74637937

Reply With Quote
  #2  
Old February 10th, 2003, 11:19 AM
arnorg arnorg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 13 arnorg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This is a very bad article, it looks like you havenīt checked that session manipulation has changed in php 4.2 version and you are stuck in php3...

You arenīt checking for the session var $_SESS[user]; but $user, and thatīs why anyone can view a member area of the page, by posting a var or slice it in the ?url query.


When a user submits information, you check just for $user but not $_GET[user] so we donīt know is itīs a form var or just a var thatīs has been sliced with the url?user=admin


Very unsecure code that you use in this article..iīm sorry i you take review badly.

Reply With Quote
  #3  
Old February 11th, 2003, 05:36 AM
Jeb. Jeb. is offline
"l33t? What's l33t?"
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 51 Jeb. User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to Jeb.
If you weren't a PHP beginner, I probably would've been saying 'Why the hell aren't you using $_SESSION???'

But, all arrogance aside, the best way to learn is to teach...The structure of the article is good. It teaches you what you need to know and does so effectively. It's got everything necessary for a beginner to learn simple authentication and session handling.

However, as arnorg so eloquently mentioned (), it does need to be updated to support PHP 4.3.0. Instead of session_register, session_is_registered and session_unregister, use $_SESSION, isset($_SESSION['varName']) and unset($_SESSION['varName']).

register_globals is a dangerous thing - best outsmart it with the autoglobals

If you change these couple of things, the article will be improved exponentially

-Jeb.
__________________
Jeb.

AIM: JebediahMc - PM Me - E-Mail Me


Reply With Quote
  #4  
Old February 13th, 2003, 02:36 AM
mbennett mbennett is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 mbennett User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
how could I use this article to redirect a user to a different page like if user_id is 3 then you will go to /page.php?id=3


TIA

Reply With Quote
  #5  
Old February 15th, 2003, 09:31 AM
nicat23's Avatar
nicat23 nicat23 is offline
Addicted to Chaos..
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: Ft. Worth, TX
Posts: 653 nicat23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 34 sec
Reputation Power: 0
Send a message via AIM to nicat23 Send a message via Yahoo to nicat23
Quote:
Originally posted by mbennett
how could I use this article to redirect a user to a different page like if user_id is 3 then you will go to /page.php?id=3


TIA


Tia:
You could do this.. however it might be in your better interest to do something like this:

In the database of users have something such as an authlevel field, and have normal users set to 1, moderators set to 2, admins set to 3, or so on.. that way when they load the page, you can have something like this function to get that access level:

PHP Code:
function getAccLvl(){       
    
session_start();
    
$userpass md5($_SESSION['userpw']);
    
$username $_SESSION['usernm'];
    @
$svrConn mysql_connect("localhost""dbUser""dbUserPass") or die ("Couldn't connect to the db server.\n");
    if(
$svrConn)
    {
        
$dbConn mysql_select_db("databasename"$svrConn) or die("Could not connect to the database<br>");
        
        if(
$dbConn)
        {
            
$strQuery "select * from users ";
            
$strQuery .= "where username = '$username'";
            
$strQuery .= " and password = '$userpass'";

            
$results mysql_query($strQuery);
            
$result mysql_fetch_row($results);
            if (
$result){
                return 
$result[3];
            }
            else{
                return 
"0";
            }
        }
    }



I know it's not the best function, but for all intensive reasons it works for what you would need. The structure of the database may vary for you though.. Mine is set like this:

Field 1 - Auto incriment userId Field2 - The username Field 3 - The users MD5'd password Field4 - the auth level
So that should explain the return $result[3]; <It would be equal to field 4 in the database>.. and if they aren't logged in it returns 0 for guest.

You can get the access level and assign it to a variable like so:
PHP Code:
 $accLvl getAccessLevel(); 


Then from there, since you have an access level of the person, whomever they are <The authlevel field is set to default of 1> you can do a case statement if you want or a redirect, whichever..

for example
PHP Code:
switch($accLvl){
    case 
3:{
        
//header('location: whatevertheadminpageis.php');
        
break;
    }
    case 
2:{
        
//header('location: whateverthemoderatorpageis.php');
        
break;
    }
    case 
1:{
        
//header('location: whateverthegeneralpageis.php');
        
break;
    }
    case 
0:{
        
//header('location: /');
    
}



not that you have to do it that way, but it could make it easier on yourself and that way you dont have the inherant risk of people trying to forge themselves as an administrator, for example..

Reply With Quote
  #6  
Old February 15th, 2003, 09:31 AM
nicat23's Avatar
nicat23 nicat23 is offline
Addicted to Chaos..
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: Ft. Worth, TX
Posts: 653 nicat23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 34 sec
Reputation Power: 0
Send a message via AIM to nicat23 Send a message via Yahoo to nicat23
Sorry for the bigass post too :P

lol.. Hope that helped..

And btw, this was a decent beginners article for sessions

Reply With Quote
  #7  
Old February 17th, 2003, 05:19 PM
Tiny Tiny is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Very north of Sweden
Posts: 2 Tiny User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm so glad someone could finally create something for us (forever) beginners!
I have encountered the same arrogance when it comes to *some* of those who allready know the scripting language. But most people are very generous and helpful.

So please! keep up the good work and be sure that a bunch of us who know and understand less than little are very pleased.

Also, I don't feel like learning all that much of the php scripting. So I'm also glad there is a wysiwyg editor for php. And why don't I want to learn, some may ask? Because I have my head full of game making scripts so there simply isn't any more room. Plus I'm an old lady, which isn't an excuse I guess. But still...

Reply With Quote
  #8  
Old February 21st, 2003, 05:12 PM
skatablaze skatablaze is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: cincinnati
Posts: 8 skatablaze User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to skatablaze
Unhappy Log IN forms

When i goto log in with a username i created in the db i get this when i hit log in. I think its because the tutorial showed older script and its not working with the new.Any help on this would be greatly appriciated

Warning: session_start() [function.session-start]: open(/tmp\sess_247cebe1c0bc5e837b12f4919cec1dc2, O_RDWR) failed: No such file or directory (2) in C:\Documents and Settings\UserX001\My Documents\My Webs\login.php on line 48

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Documents and Settings\UserX001\My Documents\My Webs\login.php:7) in C:\Documents and Settings\UserX001\My Documents\My Webs\login.php on line 48

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Documents and Settings\UserX001\My Documents\My Webs\login.php:7) in C:\Documents and Settings\UserX001\My Documents\My Webs\login.php on line 48

Notice: Array to string conversion in C:\Documents and Settings\UserX001\My Documents\My Webs\login.php on line 49

Fatal error: Call to undefined function: array() in C:\Documents and Settings\UserX001\My Documents\My Webs\login.php on line 49

Warning: Unknown(): open(/tmp\sess_247cebe1c0bc5e837b12f4919cec1dc2, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0

Reply With Quote
  #9  
Old February 21st, 2003, 05:29 PM
nicat23's Avatar
nicat23 nicat23 is offline
Addicted to Chaos..
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: Ft. Worth, TX
Posts: 653 nicat23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 34 sec
Reputation Power: 0
Send a message via AIM to nicat23 Send a message via Yahoo to nicat23
it looks like it's trying to write to the /tmp directory <which is what it would be for a unix machine>

Look in your php.ini file for
Code:
 session.save_path = /tmp


and change it to something like
Code:
 session.save_path = c:\temp 


and see if that works..

as for the other errors, put ob_start(); at the beginning of your php file and ob_flush(); at the very end

Hope that helps.

Reply With Quote
  #10  
Old February 22nd, 2003, 01:20 AM
skatablaze skatablaze is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: cincinnati
Posts: 8 skatablaze User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to skatablaze
Question sorry to bother you again

i got the error.

Parse error: parse error, unexpected T_STRING in C:\Documents and Settings\UserX001\My Documents\My Webs\login.php on line 38

Heres the whole on the page thats giving me a problem:

<?PHP
ob_start();
//check that the user is calling the page from the login form and not accessing it directly
//and redirect back to the login form if necessary
if (!isset($username) || !isset($password)) {
header( "Location: http://skata.kicks-ass.net/index.php");
}
//check that the form fields are not empty, and redirect back to the login page if they are
elseif (empty($username) || empty($password)) {
header( "Location: http://skata.kicks-ass.net/index.php");
}
else{

//convert the field values to simple variables

//add slashes to the username and md5() the password
$user = addslashes($_POST['username']);
$pass = md5($_POST['password']);


//set the database connection variables

$dbHost = "localhost";
$dbUser = "ODBC";
$dbDatabase = "members";

//connet to the database

$db = mysql_connect("$dbHost", "$dbUser") or die ("Error connecting to database.");

mysql_select_db("$dbDatabase", '$db') or die ("Couldn't select the database.");
$result = mysql_query(select * from users where username='$user' AND password='$pass', '$db');

//check that at least one row was returned

$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
while($row = mysql_fetch_array($result)){

//start the session and register a variable

session_start();
$_SESSION('$user');

//successful login code will go here...
echo 'Success!';

//we will redirect the user to another page where we will make sure they're logged in
header( "Location: check.php" );

}

}
else {

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

echo 'Incorrect login name or password. Please try again.';
}
} ob_flush(); ?>

Reply With Quote
  #11  
Old February 22nd, 2003, 09:27 AM
nicat23's Avatar
nicat23 nicat23 is offline
Addicted to Chaos..
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: Ft. Worth, TX
Posts: 653 nicat23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 34 sec
Reputation Power: 0
Send a message via AIM to nicat23 Send a message via Yahoo to nicat23
first I would recomend that you put session_start(); at the very beginning of the code...
second, I would recomending using ob_flush(); before you redirect... IE:
PHP Code:
echo "Success!";
ob_flush();
header("Location: check.php"); 


Also.. change $_SESSION('$user'); to $_SESSION['$user'];
Square brackets indicate an array..

see if that helps.

Reply With Quote
  #12  
Old February 22nd, 2003, 12:28 PM
skatablaze skatablaze is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: cincinnati
Posts: 8 skatablaze User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to skatablaze
the idiot is back again!

the error:
Parse error: parse error, unexpected T_STRING in \welcome.php on line 51


always points to this line:

$result = mysql_query(select * from users where username='$user' AND password='$pass', '$db');


sorry for bothering you justin but i am not the brightest crayon in the box.

Reply With Quote
  #13  
Old February 22nd, 2003, 01:18 PM
huezer huezer is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 1 huezer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i am a complete noobie with php. i have an FTP server host that i rent from. it has apache on it i suppose, but i am unaware if it has mysql. this article is intended for noobies such as myself, however it just jumps straight into the mysql stuff without explainin it! ;o Can someone please help me get on the right track? i.e. what i have to do to get to the mysql user db thing or w/e...... IM SO LOST!

Reply With Quote
  #14  
Old February 22nd, 2003, 01:38 PM
nicat23's Avatar
nicat23 nicat23 is offline
Addicted to Chaos..
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: Ft. Worth, TX
Posts: 653 nicat23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 34 sec
Reputation Power: 0
Send a message via AIM to nicat23