Simple login to check name and redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonathanchacon
Forum Newbie
Posts: 3
Joined: Mon Sep 14, 2009 4:55 pm

Simple login to check name and redirect

Post by jonathanchacon »

Hi everyone, first of all I am not well versed in php code so the more help the better. What I need is simple, I need a small page where a user can enter their first and last names and click submit. After entering their names, it checks the database (or preferably a text or csv file) and if they have entered their name before then it takes them to a different page than if they haven't entered their name before. I hope that makes sense.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Simple login to check name and redirect

Post by Mirge »

So, what have you tried so far? Post some code.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple login to check name and redirect

Post by califdon »

jonathanchacon wrote:Hi everyone, first of all I am not well versed in php code so the more help the better. What I need is simple, I need a small page where a user can enter their first and last names and click submit. After entering their names, it checks the database (or preferably a text or csv file) and if they have entered their name before then it takes them to a different page than if they haven't entered their name before. I hope that makes sense.
Let me explain. This is not a place for people to come to have scripts written for them. It is a place to come when you have a specific problem that you can't solve. Nobody has the time to construct a custom script, no matter how "simple" the requestor may believe it is. If you want ready-made scripts, I suggest you try places like hotscripts.com and others like that. When you have really tried to write a script and there are a few things that aren't working properly, come back here and show us what you have done and we will probably be able to help you.
jonathanchacon
Forum Newbie
Posts: 3
Joined: Mon Sep 14, 2009 4:55 pm

Re: Simple login to check name and redirect

Post by jonathanchacon »

Mirge wrote:So, what have you tried so far? Post some code.
I actually haven't tried anything so far, I've looked at some code online but nothing really helps with what I need, I know there is a simple way at going about this.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Simple login to check name and redirect

Post by Mirge »

Well, I'd recommend MySQL if you have access to it (why wouldn't you??)... especially for expandability.

If you prefer a flatfile DB (plain text)... that's do-able too of course. Which parts specifically are you having trouble with?
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Simple login to check name and redirect

Post by Mirge »

For a flatfile DB, you could do something along the lines of having each line being a first name, last name, and URL to redirect to... delimited by whatever, for example:

Code: Select all

 
John    Doe http://www.google.com/
Jane    Doe http://www.yahoo.com
 
FirstName<tab>LastName<tab>URL

The delimiter would be a tab (\t).

I would recommend reading the file line by line until you come across a match... and when you come across a match, stop reading the file (since there's no need to see what is beyond it, you'd just be wasting resources).

If no match, have some default action ready (ie: signup form)... if there is a match, then redirect them to the URL assigned to them.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Simple login to check name and redirect

Post by jackpf »

A database would be much more efficient than crawling through the file though, since it's indexed and stuff...
jonathanchacon
Forum Newbie
Posts: 3
Joined: Mon Sep 14, 2009 4:55 pm

Re: Simple login to check name and redirect

Post by jonathanchacon »

So I have decided to use a database and this is what my code looks like for the main login area:

Code: Select all

<html>
<body>
<p style="width:300px;text-align:center;font-weight:bold;">
 
</p>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Survey Connect</strong></td>
</tr>
<tr>
<td width="1200">First & Last Name</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Last 4 of SSN</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
And here is the checklogin.php file:

Code: Select all

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table 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'];
 
// 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 $tbl_name WHERE username='$myusername' and password='$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:url_for_newlogin");
}
else {
header("location:url_for_previouslogin");
}
?>
So will this give someone a different page depending on if they have logged in before or not?
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Simple login to check name and redirect

Post by Mirge »

jonathanchacon wrote:So I have decided to use a database and this is what my code looks like for the main login area:

Code: Select all

<html>
<body>
<p style="width:300px;text-align:center;font-weight:bold;">
 
</p>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Survey Connect</strong></td>
</tr>
<tr>
<td width="1200">First & Last Name</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Last 4 of SSN</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
And here is the checklogin.php file:

Code: Select all

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table 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'];
 
// 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 $tbl_name WHERE username='$myusername' and password='$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:url_for_newlogin");
}
else {
header("location:url_for_previouslogin");
}
?>
So will this give someone a different page depending on if they have logged in before or not?
Wouldn't it be easier to try it & find out? :)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple login to check name and redirect

Post by califdon »

Most of it looks OK, but like Mirge said, get used to answering your own questions by trying it. I assume that you have Apache, MySQL and PHP installed, just run it. If it works, you don't need to ask the question, if it doesn't, try to figure out why. If you can't, THEN ask a question. This is the only way you will ever learn to code.
Post Reply