new version of PHP got me..

Codes here !

Moderators: egami, macek, gesf

Post Reply
rattlsnak
New php-forum User
New php-forum User
Posts: 3
Joined: Fri May 18, 2018 4:00 pm

Fri May 18, 2018 4:21 pm

One of my hosting servers deprecated the earlier versions of PHP and left me with one website that is not working. I successfully migrated all of my other sites over but this one is giving me trouble. It's a simple connect script that checks users against a database and if they are valid users, it will send them to one page, and if they are not, it will send them to a failed login page. The present script is this: (which has been working fine fine for 10+ years!)

session_start();
$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="xxx"; // Table name

$link= mysql_connect("$host", "$username", "$password",)or die("cannot connect");
mysql_select_db($link, "$db_name")or die("cannot select DB");

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

$query="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($query);
$rowAccount=mysql_fetch_array($result);

if($rowAccount){
$_SESSION['id'] = $rowAccount['level'];
$_SESSION['username'] = $rowAccount['username'];

header("location:xxx.php");
exit;
}
else {
header("location:xxx.php");
}

I changed it to this and the connection script works with the new mysqli but it seemes the fetch array is not working. No matter what I put into the username/password, it redirects me to the failed login page.

session_start();
$con = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');

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

$result=mysqli_query($con,"SELECT*FROM users WHERE username='$myusername' AND password='$mypassword'");
$rowAccount=mysqli_fetch_array($result);

if($rowAccount){
$_SESSION['id'] = $rowAccount['level'];
$_SESSION['username'] = $rowAccount['username'];

header("location:home.php");
exit;
}

else {
header("location:error.php");
}

And yes I know, some people prefer to use Object Orientated or PDO includes, but this is just such a simple script that i want to get working. I'm sure it's something obvious!

Thanks!

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 739
Joined: Mon Feb 22, 2016 5:52 pm

Fri May 18, 2018 11:36 pm

MySQL functions are deprecated

Code: Select all

mysql_connect("$host", "$username", "$password",)or die("cannot connect");
, so all of your function calls need to be changed, I suggest you change to PDO.

If your other sites already use PDO or MySQLi, you shouldn't have any problems changing.

rattlsnak
New php-forum User
New php-forum User
Posts: 3
Joined: Fri May 18, 2018 4:00 pm

Sat May 19, 2018 8:56 am

Yes, as stated above, I changed the call ups to MYSQLi and this site still doesnt work.. Its something in the query or the fetch array that im missing..

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 739
Joined: Mon Feb 22, 2016 5:52 pm

Sat May 19, 2018 9:33 am

Sorry, I didn't realise, that's where using code tags comes in handy.

try using var_dump after the fetch:

Code: Select all

$rowAccount=mysqli_fetch_array($result);
var_dump($rowAccount);
See what is actually returned from the fetch.

Have you checked your error logs?

rattlsnak
New php-forum User
New php-forum User
Posts: 3
Joined: Fri May 18, 2018 4:00 pm

Sat May 19, 2018 6:34 pm

I just went with an entirely new OOD Version. this works..lol..
Posting to maybe help someone else..

session_start();

$_SESSION['isAuth']=false;

$con = new mysqli(xxx', 'xxx',
'xxx', 'xxx');

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

$stmnt=$con->prepare("SELECT level,username FROM users WHERE username=? and password=?");
$stmnt->bind_param("ss", $myusername,$mypassword);
$stmnt->execute();

$result=$stmnt->get_result();

if($result->num_rows===1){
$_SESSION['isAuth']=true;
$row = $result->fetch_assoc();
$_SESSION['id'] = $row['level'];
$_SESSION['username'] = $row['username'];

}else{
if($result->num_rows!==0){
die("Weirdness!");
}
}

$con->close();

if($_SESSION['isAuth']){
header("location:home.php");
}

else {
header("location:error.php");
}

Post Reply