Coding Login script

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
overdrv69
Forum Newbie
Posts: 6
Joined: Wed Feb 18, 2009 9:50 am

Coding Login script

Post by overdrv69 »

I need help figuring out why this is not working. :banghead:


<?php
//Start session
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {

header("location: access-denied.php");
exit();
}
?>


everytime I check the login it goes to the access-denied.php. Not sure why the session is not being passed I coded that in my login-exec.php script.


if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
session_write_close();
header("location: member-index.php");
exit();


I'm calling the auth.php script an the begining of "member-index.php"

Please advise.

Thanks
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Coding Login script

Post by papa »

Do you have a session_start() on member-index.php ?
overdrv69
Forum Newbie
Posts: 6
Joined: Wed Feb 18, 2009 9:50 am

Re: Coding Login script

Post by overdrv69 »

Yes,

Here is the code.

<?php
require_once('auth.php');
?>
<!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" />
<title>Member Index</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Welcome <?php echo $_SESSION['SESS_FIRST_NAME'];?></h1>
<a href="member-profile.php">My Profile</a> | <a href="logout.php">Logout</a>
<p>This is a password protected area only accessible to members. </p>
</body>
</html>


auth.php is opening the session. Are you suggesting that I should open the another session on the member-index.php?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Coding Login script

Post by papa »

You must have that function at the start of every document.

Try add:

Code: Select all

<?php
session_start();
require_once('auth.php');
?>
 
overdrv69
Forum Newbie
Posts: 6
Joined: Wed Feb 18, 2009 9:50 am

Re: Coding Login script

Post by overdrv69 »

I did as you mentioned. Still the same. Here is my auth.php code.


<?php
//Start session
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {

header("location: access-denied.php");
exit();
}
?>


I can't figure it out.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Coding Login script

Post by papa »

Start by simple error debugging. In login-exec.php for example:

Is $result set ?
Try echoing out all the variables, $member['firstname']; and $_SESSION['SESS_FIRST_NAME']..

Break down the code and go step by step.
overdrv69
Forum Newbie
Posts: 6
Joined: Wed Feb 18, 2009 9:50 am

Re: Coding Login script

Post by overdrv69 »

Ok, will try to firure out thanks.
jason.carter
Forum Commoner
Posts: 35
Joined: Sat Jan 10, 2009 10:05 am

Re: Coding Login script

Post by jason.carter »

one thing is clear.

If header("location: access-denied.php"); is called then obviously the if loop is entered.

I suggest you replace the header above with echo $_SESSION['SESS_MEMBER_ID'] and you will see it is blank.

Can you also confirm you dont have cookies OFF on your browser?
overdrv69
Forum Newbie
Posts: 6
Joined: Wed Feb 18, 2009 9:50 am

Re: Coding Login script

Post by overdrv69 »

Not sure what to do.
Last edited by overdrv69 on Wed Feb 18, 2009 5:30 pm, edited 1 time in total.
overdrv69
Forum Newbie
Posts: 6
Joined: Wed Feb 18, 2009 9:50 am

Re: Coding Login script

Post by overdrv69 »

You were right it is blank. Does that mean I cause a "if" loop?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Coding Login script

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
Post Reply