home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

help with code

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1481
Discussions: 200,931, Posts: 2,379,162, Members: 246,297
Old July 1st, 2003, 08:45 AM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Jun 2003
Posts: 11
help with code

hey... just wondering if anyone could help me with what i thought was supposed to be an easy code.. hehe...

it's just 3 pages of code: 1) index.php where it will check for page 2) which validates whether or not there is a registered session and if there isn't, it'll lead you to page 3) which is a logon page

I have a database (mysql) named "testdb" with a table called "members" with four columns ("nick" "password" "fname" and "lname")

now my trouble is...
1) if i load up the index.php it'll tell me that "The page cannot be displayed" as if the page didn't exist... if i take out the:
require("validate.php");
then the page works fine (gets data with no problems)
2) when i load up the login page by itself (never got the page to lead me into the login obviously)... i fill in the nick and password... but no matter what i do.... it just loads the login page.... even with the correct nick and password

any help would be great... thanx.. : )

here's the code:

1) index.php
<?php
require("validate.php");?>
<html>
<head>
<title>Database</title>
</head>
<body>
<h2>Current People</h2>
<table border="1">
<tr>
<td>Name</td>
</tr>
<tr>
<?php
$conn = mysql_connect();
mysql_select_db("testdb",$conn);
$query = "SELECT * FROM members";
$results = mysql_query($query, $conn);
while ($row=mysql_fetch_array($results)) {

print "
<tr>
<td>$row[fname] $row[lname]</a>
</td>
</tr>";
}mysql_close($conn);
?>
</table>

</body>
</html>




2) validate.php
<?php

session_start();

if(!(session_is_registered("userNick"))) {

header("Location:http://$HTTP_HOST/cc/login.php");

exit();

}

?>




3) login.php
<?php

if(!($conn=mysql_connect())) {
print "<BR>Error connecting...<BR>";
exit();
}

if(!mysql_select_db("testdb", $conn)) {
print "<BR>Error selecting datable.";
print mysql_errno($conn). " ". mysql_error($conn);
exit();
}

session_start();

if($nickname!="") {
$sql="SELECT * From members
WHERE nick='$nickname'";
$result=mysql_query($sql, $conn);
if(($row=mysql_fetch_array($result)) && ($password==$row[password]
&& $password!="")) {
$userNick=$nickname;
session_register("userNick");
mysql_close($conn);

header("Location:http://$HTTP_HOST/cc/index.php");
exit();
} else {
mysql_close($conn);
header("Location:http://$HTTP_HOST/cc/login.php");
exit();
}
}

mysql_close($conn);

?>

<Html>
<Head>
<Title>Login</Title>
</Head>
<Body>

<H3>Please Login</H3>

<Form Action="<?php print $PHP_SELF;?>" Method="post">
<Table>
<TR>
<TD>Nick</TD>
<TD><Input Type="Text" Name="nickname" Size="20"></TD>
</TR>

<TR>
<TD>Password</TD>
<TD><Input Type="Password" Name="password" Size="20"></TD>
</TR>

<TR>
<TD Colspan="2" Align="Center"><Input Type="Submit" Value="Login"></TD>
</TR>

</Table>
</Form>
</Body>
</Html>
kydo76 is offline   Reply With Quote
Old July 1st, 2003, 04:27 PM     #2 (permalink)
Member
 
gothic's Avatar
 
Join Date: Oct 2001
Location: Palatine, IL
Posts: 375
You should try echo'ing $HTTP_HOST to make sure the variable exists..

Also, are these pages located in yoursite.com/cc/* ?

Also, if you are using IE, make sure you turn off "Friendly error messages" .. That will make things oh-so-much easier.
gothic is offline   Reply With Quote
Old July 1st, 2003, 10:48 PM     #3 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
try slapping a print or echo in validate.php as first line, then you will know if it is actually going there.

also, you need a session_start as first line in every page you want to use session stuff, or you used to have to do that.

your login.php, which has reference to self in FORM, doesn't have any code to handle submission of form!

use:

if (isset($_POST['password']))
{
//login success, show em something
else
{
//show form to login
}

Lastly using header to redirect is not the ideal way to accomplish branching logically (many reasons), better to use includes inside conditionals.
qball is offline   Reply With Quote
Old July 2nd, 2003, 05:09 AM     #4 (permalink)
Junior Member
 
Join Date: Jun 2003
Posts: 11
thanks for the help... i finally got it to work...

it was all because of the version change... what a pain... anyway... what i did was change some stuff in the login.php... anyone who's curious, what i did was:
1) change each $HTTP_HOST to {$_SERVER['HTTP_HOST']}
2) for each variable passed by the post method, type the variable in the form of $_POST['variable_name']
3) in line 15, I had the "!" in the wrong place

here's the corrected code for login.php if anyone wants:

<?php
session_start();
if(!($conn=mysql_connect())) {
print "<BR>Error connecting...<BR>";
exit();
}

if(!mysql_select_db("testdb", $conn)) {
print "<BR>Error selecting datable.";
print mysql_errno($conn). " ". mysql_error($conn);
exit();
}


if($_POST['nickname']!="") {
$sql="SELECT * From members
WHERE nick='".$_POST['nickname']."'";
$result=mysql_query($sql, $conn);
if(($row=mysql_fetch_array($result)) && ($_POST['password']==$row[password]
&& $_POST['password']!="")) {
$userNick=$_POST['nickname'];
session_register("userNick");
mysql_close($conn);

header("Location:http://{$_SERVER['HTTP_HOST']}/cc/index.php");
exit();
} else {
mysql_close($conn);
header("Location:http://{$_SERVER['HTTP_HOST']}/cc/login.php");
exit();
}
}

mysql_close($conn);

?>

<Html>
<Head>
<Title>Login</Title>
</Head>
<Body>

<H3>Please Login</H3>

<Form Action="<?php print $PHP_SELF;?>" Method="post">
<Table>
<TR>
<TD>Nick</TD>
<TD><Input Type="Text" Name="nickname" Size="20"></TD>
</TR>

<TR>
<TD>Password</TD>
<TD><Input Type="Password" Name="password" Size="20"></TD>
</TR>

<TR>
<TD Colspan="2" Align="Center"><Input Type="Submit" Value="Login"></TD>
</TR>

</Table>
</Form>
</Body>
</Html>
kydo76 is offline   Reply With Quote
Old July 2nd, 2003, 05:14 AM     #5 (permalink)
Junior Member
 
Join Date: Jun 2003
Posts: 11
qball.... u said "Lastly using header to redirect is not the ideal way to accomplish branching logically (many reasons), better to use includes inside conditionals."

i'm not exactly sure how it would be better, but thanx for the tip... i'll try it out once i get better at PHP.. hehe... uhm... you wouldn't happen to have any articles or examples of the above said that you can reference me, would u? anyway, thanx again
kydo76 is offline   Reply With Quote
Old July 3rd, 2003, 01:40 AM     #6 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
Quote:
i'm not exactly sure how it would be better

using a header to redirect browser, can be like opening new browser window to 'URL'. relies on client.

better to rely on server.
qball is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2874)
Obama the Muslim (14)
California Passes Anti-Flat-HDTV Le.. (39)
Is the PSU I received dead? (10)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
HIS HD5770 graphic card question (15)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
Ideal cheap graph card for PC-Gamin.. (16)
EVGA 9800 gtx help with finding a g.. (8)
New Computer wont recognize XP disc (7)
Recent Discussions
Wireless Televisions. (0)
Install XP pro and a Vista laptop ?? (11)
Partition Magic caused HDD problem (2)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Regular Build (3)
Ideal cheap graph card for PC-Gaming? (16)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Have you switched yet? (84)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)
Foreign voltage (10)
FiOS modem/router interfering with ne.. (7)


All times are GMT -4. The time now is 08:10 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28