PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,005 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,997 people online right now. Registration is fast and FREE... Join Now!




SQL server issues...

 

SQL server issues..., Logging

guyfromri

2 Nov, 2009 - 07:27 PM
Post #1

New D.I.C Head
*

Joined: 16 Sep, 2009
Posts: 18


My Contributions
Ok, so this is probably realllly easy and I'm just missing it. I'm new to PHP but have experience in VB and HTML and some Java. I am linking the login script to my SQL database of users. I have 2 questions. One, the $host""....I have seen people specify "mysql" or "localhost" but I know thats wrong. I do have a server address of "dbname.db.######.hostedsource.com". Is that my host? If not, does anyone know where abouts I should look? I posted the code so you would have a better idea.

Question 2: This being my first login script...how do I make pages login_specific...meaning how do I re-route to the login screen if the session is killed. Is there a certain code I put at the top of each page or do I just put them all in one directory?

THANKS IN ADVANCE FOR ANY HELP!!!!

CODE

<html>
<head></head>
<body>
<?php
$host="????.???????.??.????????.????"; // Host name
$username="check"; // Mysql username
$password="check"; // Mysql password
$db_name="check"; // 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:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
</body>
</html>


User is offlineProfile CardPM
+Quote Post


cmwise

RE: SQL Server Issues...

2 Nov, 2009 - 08:59 PM
Post #2

D.I.C Head
**

Joined: 14 Feb, 2009
Posts: 149



Thanked: 5 times
My Contributions
Probably just hostedsource.com if I had to guess... otherwise I'm not quite sure.

And for your second question, a simple check to see if the session is created or not at the top of the page should do it:
CODE

session_start();
if(!isset($_SESSION['username'])){
header("Location:login.php");
}


This is obviously just an example and not substitutable into your specific code.

Also, for security purposes, I'm fairly sure you no longer need to use stripslashes. And you could compress your code by doing something like this:
CODE

$myusername = mysql_real_escape_string($_POST['myusername']);
$mypassword = mysql_real_escape_string($_POST['mypassword']);


Also I'm not sure what the "session_register" call does, I've never personally used it or seen it for that matter.

Other than that your code looks like it should work, although it's late and I might have missed something :-X

Hope this all helps!!


This post has been edited by cmwise: 2 Nov, 2009 - 09:03 PM
User is offlineProfile CardPM
+Quote Post

guyfromri

RE: SQL Server Issues...

4 Nov, 2009 - 01:24 PM
Post #3

New D.I.C Head
*

Joined: 16 Sep, 2009
Posts: 18


My Contributions
Ok, so I actually called my host and asked for the host name. They gave me the name "databasename.db.#####.hostedresource.com". It still doesn't work in my script. The sql is set up right and the php should be working. I cant get the code past the database login to even check my info against the db. Any ideas? Silly question, but should I be using "http://"?
User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: SQL Server Issues...

4 Nov, 2009 - 01:56 PM
Post #4

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
session_register() should not be used its deprecated and on its way out the door ..

instead just set your sessions use the $_SESSION array

CODE

$_SESSION['username'] = "myusername";
$_SESSION['password'] = "mypassword"; // this should be avoided the only reason to deal with a password is for comparison not storage


Some hosts actually have a dedicated mysql database server reason for the odd address mine is on the same box so "localhost" works just fine ...

i did test mine from my local machine and i did not have to use http:// to get it to work remotely

code tested ( of course its a mysqli connection not a basic mysql connection):
CODE

// Create the DB connection
// change these values to what you need...
$mysqli = @new mysqli('yourdatabasename.db.sitename.hostservice.com', 'full_db_username', 'db_password', 'full_database_name');
    if ($mysqli->connect_error) {
        echo "Connect Error (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . ")";
    } else {
            echo "CONNECTED!!!";
    }


If your not seeing this error, per your code "cannot connect" than you SHOULD be connected ... the die statement should take care of at least a crude error checking method BUT i would add mysql_error() because it can display some pretty use information as to WHY your not connected correctly ...

CODE

mysql_connect("$host", "$username", "$password")or die("cannot connect, error: " . mysql_error());


This post has been edited by RPGonzo: 4 Nov, 2009 - 02:14 PM
User is offlineProfile CardPM
+Quote Post

guyfromri

RE: SQL Server Issues...

5 Nov, 2009 - 12:51 PM
Post #5

New D.I.C Head
*

Joined: 16 Sep, 2009
Posts: 18


My Contributions
Thank you thank you thank you for the error return. Very helpful. I have been tweeking the code now and it works....except for this

cannot connect error: Client does not support authentication protocol requested by server; consider upgrading MySQL client

I'm not sql literate yet...I'm taking it that My sql db doesn't support php login scripts and outside connecting?
User is offlineProfile CardPM
+Quote Post

guyfromri

RE: SQL Server Issues...

5 Nov, 2009 - 01:04 PM
Post #6

New D.I.C Head
*

Joined: 16 Sep, 2009
Posts: 18


My Contributions
QUOTE(guyfromri @ 5 Nov, 2009 - 12:51 PM) *

Thank you thank you thank you for the error return. Very helpful. I have been tweeking the code now and it works....except for this

cannot connect error: Client does not support authentication protocol requested by server; consider upgrading MySQL client

I'm not sql literate yet...I'm taking it that My sql db doesn't support php login scripts and outside connecting?



I TOTALLY TAKE THAT BACK. I FIXED IT....SILLY ISSUE...MYSQL 5.0 USING A DIFFERENT ALGORITHIM STRING FOR PASSWORDS. I HAD TO GO TO MY MYSQL AND USE THIS CODE..HOPE IT HELPS SOMEONE SOME DAY.

CODE

SET PASSWORD = OLD_PASSWORD('MyPassword');


THANK YOU ALL SO MUCH FOR THE HELP

This post has been edited by guyfromri: 5 Nov, 2009 - 01:05 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:54AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month