Login question php??

Thread Solved

Join Date: Mar 2008
Posts: 63
Reputation: sukhy_1 is an unknown quantity at this point 
Solved Threads: 3
sukhy_1 sukhy_1 is offline Offline
Junior Poster in Training

Login question php??

 
0
  #1
Mar 20th, 2008
Hello ive got this code of the net just wanted some1 2check it 2make sure, also ive got these two peices of code the ones ive highlighted them in red, but dnt understand wa it means by saying put this code in first line of web page any ideas, ive got the basic loggin form which is linked from homepage. any 1 got any ideas?? thanks dave

// Put this code in first line of web page.
<?
session_start();
session_destroy();
?>


// Check if session is not registered , redirect back to student login.
// Put this code in first line of web page.
<?
session_start();
if(!session_is_registered(myusername)){
header("locationtudentLogin.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>
-----------------------------------------------------------------------------------------------

<?php
$host="host"; // Host name
$username="dk"; // Mysql username
$password="d"; // Mysql password
$db_name=""; // Database name


// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// encrypt password
$encrypted_mypassword=md5($mypassword);

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM members WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Re: Login question php??

 
0
  #2
Mar 20th, 2008
Hi there, I'm a bit confused:
  • I don't see a reason for calling session_start(); session_destroy(); I'd say that you can safely ignore this
  • The other "Put this code in first line of web page" is correct though. All your protected web pages must have .php extension and contain this as the first thing in the file
    1. <?php
    2. session_start();
    3. if (!session_is_registered("myusername")) {
    4. header("locationtudentLogin.php");
    5. }
    6. ?>
Otherwise the code seems to be ok, let me know if you're having any problems.
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: sukhy_1 is an unknown quantity at this point 
Solved Threads: 3
sukhy_1 sukhy_1 is offline Offline
Junior Poster in Training

Re: Login question php??

 
0
  #3
Mar 21st, 2008
Hello so your saying put that code in everypage that needs a password and username 2access it but call the page something like homepage.php, and the code above goes in the top section. so when someone logs in it takes them 2 a page with this code in "Put this code in first line of web page" am i right thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Re: Login question php??

 
0
  #4
Mar 21st, 2008
I'm sorry I got lost in your post so let me reword it.
Let's say that you have pages homepage.php, courses.php, students.php and you want to protect all three - valid username+password is required to access them.
So you put the session_start()... code into each.

When someone opens one of them and is not logged in, he will be redirected to locationtudentLogin.php - that's the page with $host=...
He will log in there and stay logged in even when he leaves the page. Then he can access one of the protected page and he will see their content.

Is it clearer now?
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Login question php??

 
0
  #5
Mar 21st, 2008
Basically, its like this. You have for example 3 pages. homepage.php, courses.php and students.php.
homepage.php is a login script, where the user can login. You should check if the user is a valid user. You shouldn't let the user to go to page2 or page3 (ie., courses.php and students.php) if the user hasn't logged in. For this, you can use sessions. Once the user has logged in, add his username to the session. ($_SESSION['username']=$username; ). Then on page2 and page3, check if $_SESSION['username'] is not null. If its null, then he isn't a valid user, so redirect him back to homepage.php. If he's a valid user, show him page2. That can be done as shown by petr.pavel in post2.
Last edited by nav33n; Mar 21st, 2008 at 9:49 am.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: sukhy_1 is an unknown quantity at this point 
Solved Threads: 3
sukhy_1 sukhy_1 is offline Offline
Junior Poster in Training

Re: Login question php??

 
0
  #6
Mar 21st, 2008
k ive kind of got it now so im gona put this code in the first page after the user logs in..
<?phpsession_start();if (!session_is_registered("myusername")) { header("locationtudentLogin.php");}?>

Then after his filled in the first form he will go 2the next screen where i need 2put this code in and so on and on page 3

$_SESSION['username']
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: sukhy_1 is an unknown quantity at this point 
Solved Threads: 3
sukhy_1 sukhy_1 is offline Offline
Junior Poster in Training

Re: Login question php??

 
0
  #7
Mar 21st, 2008
when user signs in keeps saying "Wrong Username or Password";
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Re: Login question php??

 
0
  #8
Mar 21st, 2008
Originally Posted by sukhy_1 View Post
when user signs in keeps saying "Wrong Username or Password";
So what did you do to try to find out why? C'mon, we're not going to do your homework for you, we can just explain things if you ask specific questions.

Try to insert echo $sql; after $sql="SELECT * FROM members WHERE username='$myusername' and password='$encrypted_mypassword'"; to see what's being executed. Then you can execute the sql yourself and see if it returns something.
This way you will verify that:
* username and password is properly submitted
* a db record for this user really exist - and there's only one, not more of them
* the query is correct

My impression is that you're just starting with PHP and you're trying to jump right into the middle of a project without learning the basics first.
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 694 | Replies: 7
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC