Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-31-2011, 08:25 PM   PM User | #1
tkm76
New Coder

 
Join Date: Jan 2011
Posts: 29
Thanks: 4
Thanked 0 Times in 0 Posts
tkm76 is an unknown quantity at this point
Direct login (from index.php to member.php)

Hi everyone,

I have these php files:

index.php
login.php
member.php
logout.php


Basically, I would like to be able to login directly to member.php, instead seeing a message "You are now logged in" on login.php and then needing to click to proceed. I don't find that suitable or necessary at all. But this is how tutorial instructed me to code.
I have tried many things, but I'm not getting very far with this since I'm a newbie, so if anyone has an advice or a solution, I would be most grateful...
Below are my files for reference:


---/index.php/----------------------------------------/index start/---

<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<?php
echo "<p class='title'>example.com / Demo / User Log In</p>
<p class='lastedited'><a href='http://www.example.com'>Home</a> | <a href='http://www.example.com/demo'>Back to Demo</a> | <b>Last updated on March 29, 2011</b></p><br />";
echo "<br /><br /><br />";
echo "<div align='center' style='width: 300px; background-color: #FFF; border: 1px solid #0087D4; margin: 0px auto 0px auto; padding: 20px 0px 20px 0px; font-family: arial; font-size: 12px'>"."
<form action='login.php' method='POST'><br /><br />
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<br />
<input type='submit' value='Log in'>
</form>";
?>
</body>
</html>

---/index.php/----------------------------------------/index end/---


---/login.php/----------------------------------------/login start/---

<?php
session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if ($username && $password)
{
$connect = mysql_connect("localhost","******","*****") or die ("Could not connect");
mysql_select_db("session") or die ("Could not find db");

$query = mysql_query("SELECT * FROM login WHERE username = '$username'");

$numrows = mysql_num_rows($query);

if ($numrows != 0)
{

while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
// check to see if they match!
if ($username == $dbusername && $password == $dbpassword)
{
echo "You are now logged in! <a href='member.php'>Click here to enter members area.</a>";

$_SESSION['username']=$username;
}
else
echo "<div style='font-family: Verdana; font-size: 12px;'>"."Incorrect password!"."</div>";
}
else
die("<div style='font-family: Verdana; font-size: 12px;'>"."No such user in database!"."</div>");

}
else
die ("<div style='font-family: Verdana; font-size: 12px;'>"."Please enter username and password!"."</div>");
?>
---/login.php/----------------------------------------/login end/---


---/member.php/---------------------------------/member start/---

<?php
session_start();

if ($_SESSION['username'])

echo "<div style='font-family: Verdana; font-size: 12px; border: 1px solid #43A5EC; background: #F0F0F0; padding: 10px;'>"."Welcome, "."<b>".$_SESSION['username']."</b>"."<b>!</b>";
else
die("<div style='font-family: Verdana; font-size: 12px;>"."You must be logged in!"."</div>");
echo " | "."<a href='http://localhost/login/change.php'>Change password</a> | <a href='http://localhost/login/change.php'>Forgot password?</a> | <a href='logout.php'>Logout<a/>"."</div>";
?>

---/member.php/---------------------------------/member end/---


---/logout.php/---------------------------------/logout start/----

<?php
session_start();
session_destroy();
echo "<div style='font-family: Verdana; font-size: 12px; border: 1px solid #43A5EC; background: #F0F0F0; padding: 10px;'>"."You've been logged out. <a href='index.php'>Click here to return."."</div>";
?>

---/logout.php/---------------------------------/logout end/----
tkm76 is offline   Reply With Quote
Old 03-31-2011, 09:16 PM   PM User | #2
bucket
Regular Coder

 
Join Date: Sep 2008
Posts: 688
Thanks: 8
Thanked 17 Times in 16 Posts
bucket is infamous around these parts
Would be glad if you can put the code inside [php] tags...

Its the PHP icon with the page behind it on your editor at the top.


Thanks.
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
bucket is offline   Reply With Quote
Old 03-31-2011, 09:45 PM   PM User | #3
tkm76
New Coder

 
Join Date: Jan 2011
Posts: 29
Thanks: 4
Thanked 0 Times in 0 Posts
tkm76 is an unknown quantity at this point
Hi there, thanks for replying. I hope this works for you. Not really sure what the end result should be.


index.php
PHP Code:
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<?php
echo "<p class='title'>example.com / Demo / User Log In</p>
<p class='lastedited'><a href='http://www.example.com'>Home</a> | <a href='http://www.example.com/demo'>Back to Demo</a> | <b>Last updated on March 29, 2011</b></p><br />"
;
echo 
"<br /><br /><br />";
echo 
"<div align='center' style='width: 300px; background-color: #FFF; border: 1px solid #0087D4; margin: 0px auto 0px auto; padding: 20px 0px 20px 0px; font-family: arial; font-size: 12px'>"."
<form action='login.php' method='POST'><br /><br />
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<br />
<input type='submit' value='Log in'>
</form>"
;
?>
</body>
</html>
login.php
PHP Code:
<?php
session_start
();

$username $_POST['username'];
$password $_POST['password'];

if (
$username && $password)
{
$connect mysql_connect("localhost","******","*****") or die ("Could not connect");
mysql_select_db("session") or die ("Could not find db");

$query mysql_query("SELECT * FROM login WHERE username = '$username'");

$numrows mysql_num_rows($query);

if (
$numrows != 0)
{

while (
$row mysql_fetch_assoc($query))
{
$dbusername $row['username'];
$dbpassword $row['password'];
}
// check to see if they match!
if ($username == $dbusername && $password == $dbpassword)
{
echo 
"You are now logged in! <a href='member.php'>Click here to enter members area.</a>";

$_SESSION['username']=$username;
}
else
echo 
"<div style='font-family: Verdana; font-size: 12px;'>"."Incorrect password!"."</div>";
}
else
die(
"<div style='font-family: Verdana; font-size: 12px;'>"."No such user in database!"."</div>");

}
else
die (
"<div style='font-family: Verdana; font-size: 12px;'>"."Please enter username and password!"."</div>");
?>
member.php
PHP Code:
<?php
session_start
();

if (
$_SESSION['username'])

echo 
"<div style='font-family: Verdana; font-size: 12px; border: 1px solid #43A5EC; background: #F0F0F0; padding: 10px;'>"."Welcome, "."<b>".$_SESSION['username']."</b>"."<b>!</b>";
else
die(
"<div style='font-family: Verdana; font-size: 12px;>"."You must be logged in!"."</div>");
echo 
" | "."<a href='http://localhost/login/change.php'>Change password</a> | <a href='http://localhost/login/change.php'>Forgot password?</a> | <a href='logout.php'>Logout<a/>"."</div>";
?>
logout.php
PHP Code:
<?php
session_start
();
session_destroy();
echo 
"<div style='font-family: Verdana; font-size: 12px; border: 1px solid #43A5EC; background: #F0F0F0; padding: 10px;'>"."You've been logged out. <a href='index.php'>Click here to return."."</div>";
?>

Last edited by tkm76; 03-31-2011 at 09:50 PM.. Reason: code error
tkm76 is offline   Reply With Quote
Old 03-31-2011, 09:49 PM   PM User | #4
bucket
Regular Coder

 
Join Date: Sep 2008
Posts: 688
Thanks: 8
Thanked 17 Times in 16 Posts
bucket is infamous around these parts
Edit your last post.

The reason its good to use php tags is because its color coded and not alot of page scrolling is needed.

You messed up on your last post.

Missing some start php tags.
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
bucket is offline   Reply With Quote
Old 03-31-2011, 09:56 PM   PM User | #5
bucket
Regular Coder

 
Join Date: Sep 2008
Posts: 688
Thanks: 8
Thanked 17 Times in 16 Posts
bucket is infamous around these parts
instead of echoing that they can now proccedd to the members area just use this:

PHP Code:
 header('Location: member.php'); 
in:
PHP Code:
if ($username == $dbusername && $password == $dbpassword)
{

PUT REDIRECT HERE

//echo "You are now logged in! <a href='member.php'>Click here to enter members area.</a>";
$_SESSION['username']=$username;

Have you ever tried a header redirect from the 'many things I tried'.
bucket is offline   Reply With Quote
Users who have thanked bucket for this post:
tkm76 (03-31-2011)
Old 03-31-2011, 10:05 PM   PM User | #6
tkm76
New Coder

 
Join Date: Jan 2011
Posts: 29
Thanks: 4
Thanked 0 Times in 0 Posts
tkm76 is an unknown quantity at this point
It worked! Thank you!
Yes I did use Header on several occasions, but I haven't coded php in two months. I guess I have a lot of catching up to do.

Thanks again very much!
tkm76 is offline   Reply With Quote
Reply

Bookmarks

Tags
login, page

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:13 AM.


Advertisement
Log in to turn off these ads.