Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > PHP
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2005, 09:31 AM   #1 (permalink)
JimAdkins
Registered User
 
Join Date: Mar 2005
Posts: 8
JimAdkins is on a distinguished road
Trouble with PHP Login Script

I tried useing the PHP login script that was posted here,

http://php.codenewbie.com/articles/p...ns-Page_1.html

and the test pages worked just fine. Now I'm trying to implement them into some php pages that I coded myself, and it logs the user out after 2 pages. I'm wondering if it is just looseing the session info. The pages that I am useing are not useing <a href=> links, but are submiting forms. In each of the pages I have the proper include statments, but once I log in, and get to the main page, then submit the form that takes me a different page, that page works, but then I click on a link that is supposed to take me back to my main page, and it says I'm not logged in and need to log in again. It's starting to get frustrating. Any help would be very apreciated.

Thanx in advance. -Jim
__________________
JimAdkins is offline   Reply With Quote
Old 03-19-2005, 10:08 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
hi jim, welcome to the site

have you modified auth.php at all? all the session logic which would cause the page not to load is in there.

if you have modified it, could you post the code? also, make sure there is nothing, not even white space printed to the screen by the time you include auth.php. session functions are header info and will fail if anything at all is printed to the screen before those scripts are called.

i'm not sure how closely the pages you are implementing this on follow the tutorial, so it is very hard to give you a good answer without seeing some code.
__________________
testing 1 2 3
sde is online now   Reply With Quote
Old 03-19-2005, 10:21 AM   #3 (permalink)
JimAdkins
Registered User
 
Join Date: Mar 2005
Posts: 8
JimAdkins is on a distinguished road
Cool. Thanx for the quick reply and the welcome. I have not changed the auth.php file. The only difference that I have made is on the include statements I have removed th inc/ directory, so all of my files are in the same dir. As for some code, lemme post the basic index.php page. All it is is a bunch of forms that submit to other php pages with the get type. This way the $_GET info can be passed to the other pages. I have tried adding a <a href link to the page that goes to another <a href link, and the session seems to be ok. I'm wondering if the form might not be posting correctly.

PHP Code:
<?php

// connect to database
include("connect.php");

// include auth and nav
include("auth.php");

echo 
"

<html>
<head>
<title>Input</title>
</head>
<body>
<center>
<font face=Arial size=5>
<b>Attendance</b><br><br>
<form action='attendance.php' type='get'><font face=Arial size=2>
Select Month:&nbsp&nbsp


<select name='month'>
<option value=''>Month...</option>
<option value='January'>January</option>
<option value='February'>February</option>
<option value='March'>March</option>
<option value='April'>April</option>
<option value='May'>May</option>
<option value='June'>June</option>
<option value='July'>July</option>
<option value='August'>August</option>
<option value='September'>September</option>
<option value='October'>October</option>
<option value='November'>November</option>
<option value='December'>December</option>
</select><br><br>
<input type=submit value=Submit></font>
</form>
<hr />
<b>Reports</b><br><br>
    <form action='report.php' target=_blank type=get>
    <font face=Arial size=2>
    Generate Report for the Month of:&nbsp&nbsp
    <select name='month'>
    <option value=''>Month...</option>
    <option value='January'>January</option>
    <option value='February'>February</option>
    <option value='March'>March</option>
    <option value='April'>April</option>
    <option value='May'>May</option>
    <option value='June'>June</option>
    <option value='July'>July</option>
    <option value='August'>August</option>
    <option value='September'>September</option>
    <option value='October'>October</option>
    <option value='November'>November</option>
    <option value='December'>December</option>
    </select><br><br>
    <input type=hidden name='act' value='reportgo'>
    <input type=submit value='Submit'></font>
    </form>
    <hr />

</body>
</html>
"
;
?>
Thanx again for the help. Really appreciated.
__________________
JimAdkins is offline   Reply With Quote
Old 03-19-2005, 10:27 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
try changing your form tag to this:
HTML Code:
<form method='GET' action='attendance.php'>
__________________
testing 1 2 3
sde is online now   Reply With Quote
Old 03-19-2005, 10:36 AM   #5 (permalink)
JimAdkins
Registered User
 
Join Date: Mar 2005
Posts: 8
JimAdkins is on a distinguished road
Cool. Just tried that, and got the same result. Let me post the attendance.php file. This file basically shows info from a db that can be changed to show whether that person was in atendance or not. It also has a link that goes back to the index.php page at the top of the page to help with site navigation. All I'm doing is logging in, choosing a month, it goes to the attendance page, I view the data, then click on the link that goes back to the index.php page. after I click that link, it says to log in again.

Also, I'm still very new to programming in PHP. So this may seem to be not programmed right.

PHP Code:
<?php

// connect to database
include("connect.php");

// include auth and nav
include("auth.php");


$username="***";
$password="***";
$database="***";

$month=$_GET['month'];

if (
$month == '')
{
    echo 
"<html><title>Error</title>
    <font face=Arial size=2>>&nbsp&nbsp<a href='/church/index.php'>Home</a></font><br>
    You must choose a month. Please click home and choose again.</html>"
;
    return 
EOF;
}


mysql_connect('localhost'$username$password) or die ('I cannot connect to the database because: ' mysql_error());
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT Name, $month, ID FROM attendance ORDER BY 'Name'";
$result=mysql_query($query);

$num=mysql_numrows($result);


echo 
"<html><head><title>Attendance Results</title></head>
<font face=Arial size=2>>&nbsp&nbsp<a href='/church/index.php'>Home</a></font><br>
<center><b><font face=Arial size=5>Attendance for $month</font></b><br><br>
<table border='1' cellspacing='2' cellpadding='2'>
<thead><tr><th><font face=Arial size=3><u>Name</u></font></th><th>
<font face=Arial size=3><u>Attended</u></font></th>
<th><font face=Arial size=3><u>Name</u></font></th><th>
<font face=Arial size=3><u>Attended</u></font></th>
<th><font face=Arial size=3><u>Name</u></font></th><th>
<font face=Arial size=3><u>Attended</u></font></th></thead>"
;

mysql_close();

$i=0;
while (
$i $num) {

    
$name=mysql_result($result,$i,"Name");
    
$monthht=mysql_result($result,$i,$month);
    
$id=mysql_result($result,$i,"ID");

    
$checked1="";
    
$checked0="";

    if(
$monthht==0)
        {
        
$checked0="checked";
        }
    else
        {
        
$checked1="checked";
        }
        if (
$i == OR $i == OR $i == OR $i == 12 OR $i == 15 OR $i == 18 OR $i == 21 OR $i == 24 OR $i == 27 OR $i == 30 OR $i == 33 OR $i == 36 OR $i == 39 OR $i == 42 OR $i == 45 OR $i == 48 OR $i == 51 OR $i == 54 OR $i == 57 OR $i == 60 OR $i == 63 OR $i == 66 OR $i == 69 OR $i == 72 OR $i == 75 OR $i == 78 OR $i == 81 OR $i == 84 OR $i == 87 OR $i == 90 OR $i == 93 OR $i == 96 OR $i == 99)
        {
            echo 
"<tr>";
        }
    echo 
"<td width=100 align=center><font face=Arial size=2>$name</td></font><td width=100 align=center>
    <form action='update.php?PHPSESSID=.session_id()' type=get>
    <input type=hidden name='month' value=$month>
    <input type=hidden name='id' value=$id>
    <input type=hidden name='act' value='updateatt'>
    <input type=radio name='results' onClick='this.form.submit()' value=1 $checked1><font face=Arial size=2>Yes&nbsp&nbsp</font>
    <input type=radio name='results' onClick='this.form.submit()' value=0 $checked0><font face=Arial size=2>No&nbsp&nbsp</font>
    </td></form>"
;

    
$i++;
}
echo 
"</table></center></html>"

?>
__________________
JimAdkins is offline   Reply With Quote
Old 03-19-2005, 10:48 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
is error reporting turned on? i mean, when you make errors, does it display the Warning message? it doesn't make sense to me if you are using the same auth.php script.

also, just some other tips, in PHP, you should use the operator || instead of the word OR. i guess if OR works, then it's fine, it's just more common to see the pipes. i.e.
PHP Code:
<?
if($i==|| $i==2){
  
// do this
}

// also, the and operator is &&
if($i==&& $b==2){
  
// do this
}
now, another way to complete the logic in your huge if statement, might be this:
PHP Code:
<?
// create your array of possibilities
$my_array = array(3,6,9,12,15,18,21,
                  
24,27,30,33,36,39,
                  
42,45,48,51,54,57,
                  
60,63,66,69,72,75,
                  
78,81,84,87,90,93,
                  
96,99);

if(
in_array($i,$my_array)){
  
// do something
}
?>
__________________
testing 1 2 3
sde is online now   Reply With Quote
Old 03-19-2005, 10:56 AM   #7 (permalink)
JimAdkins
Registered User
 
Join Date: Mar 2005
Posts: 8
JimAdkins is on a distinguished road
Quote:
is error reporting turned on? i mean, when you make errors, does it display the Warning message? it doesn't make sense to me if you are using the same auth.php script.
Not quite sure what you mean by error reporting. And by make errors do you mean errors in the code? or input errors on the form? And how can I turn on the error reporting?

If you want to visit the site, I'll send you the link, I would just rather not post it because I don't want everyone to see the info. Hence trying to make the login required. Do you have an email address?

Also, for the tips, MUCH THANX. All I really have to go off of with programming PHP right now is what I can find on the net. I really need to buy a book and get some of the basics down. LOL
__________________
JimAdkins is offline   Reply With Quote
Old 03-19-2005, 12:08 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
ok, quick question. on attendance.php, i see you are connecting to the database, even though you have already included connect.php.

i think defining $username and connecting to the database might be causing a problem. if this is the same database as you are connecting to in connect.php, then there is no need to use the mysql_connect functions again.

can you take this out and try again? i went to your site and attendance.php seems to be the only script that is causing the logout.
__________________
testing 1 2 3
sde is online now   Reply With Quote
Old 03-19-2005, 12:13 PM   #9 (permalink)
JimAdkins
Registered User
 
Join Date: Mar 2005
Posts: 8
JimAdkins is on a distinguished road
Ok, so you want me to remove just the connection? Lemme try that.

I commented out

Code:
mysql_connect('localhost', $username, $password) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db($database) or die( "Unable to select database");
And I still get the same thing.
__________________
JimAdkins is offline   Reply With Quote
Old 03-19-2005, 12:14 PM   #10 (permalink)
JimAdkins
Registered User
 
Join Date: Mar 2005
Posts: 8
JimAdkins is on a distinguished road
Ok, wait. I had changed it to $_POST values, but now I just changed it back to $_GET and it seems to be working. That's wierd. Why would it work with GET and not POST?
__________________
JimAdkins is offline   Reply With Quote
Old 03-19-2005, 12:20 PM   #11 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Quote:
Originally Posted by JimAdkins
Ok, wait. I had changed it to $_POST values, but now I just changed it back to $_GET and it seems to be working. That's wierd. Why would it work with GET and not POST?
Depends on what form method you use. $_GET will be populated during a standard page request ({a href}) or a form that uses a GET method. $_POST is only for forms that use a post method.

I typically only use method="GET" for forms that are requesting data and use POST for submitting data.

You can also use $_REQUEST, but be aware that any cookies will also be contained in the array.

-r
__________________
idx is offline   Reply With Quote
Old 03-19-2005, 12:31 PM   #12 (permalink)
JimAdkins
Registered User
 
Join Date: Mar 2005
Posts: 8
JimAdkins is on a distinguished road
idx, thanx for the info. It makes more sense now.

Do the include statments have to be in each function? Or just at the begining of the file?

The one function file is working good, but I have a file that has a few functions in it and it's not working right.

Edit: Wow, I should really try things before I post. If I put the include statments inside the function, it works like a charm!

Another quick question, what about a mysql_close() statment? Shouldn't the DB be closed?

Thanx again for all of your help!
__________________
JimAdkins is offline   Reply With Quote
Old 03-19-2005, 02:11 PM   #13 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Good to hear. include or require (include_once is also a good idea) can be anywhere. If you have a number of files to include then it might be worth doing it all in one file, so you only have to include 1 file in each of your pages.

You can use mysql_close(), but it's not really necessary for non-persistent connection. (they'll close automatically when the script ends)

-r
__________________
idx is offline   Reply With Quote
Old 03-19-2005, 02:17 PM   #14 (permalink)
JimAdkins
Registered User
 
Join Date: Mar 2005
Posts: 8
JimAdkins is on a distinguished road
Last question. What's the difference between require and include?

BTW, thanx sde first for making the script, and then for helping me as much as you have. Thanx idx for all of your help in answering my questions.
__________________
JimAdkins is offline   Reply With Quote
Old 03-19-2005, 02:40 PM   #15 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
not saying RTFM, .. but i really want to point out how great the php documentation is. i wasn't sure the answer to this myself, besides that they are very much alike, so i went to the php.net website and typed in require in the function search. php.net function search is a priceless resource for your php development.

http://us4.php.net/manual/en/function.require.php

Quote:
require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.
__________________
testing 1 2 3
sde is online now   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a server script from PHP sde PHP 4 02-05-2005 11:12 AM
php script that can upload binary data infinite_root PHP 9 08-27-2004 06:03 PM
PHP code for Login Screen KEEBOD PHP 2 07-15-2004 05:18 PM
narrowed down login trouble trevor PHP 7 01-18-2003 05:50 PM
login script trevor PHP 21 01-17-2003 06:28 AM


All times are GMT -8. The time now is 11:54 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle