'Login with PHP/SQL Server not connecting
I'm migrating an app from MySql to SQL Server. The thing is that im trying to make the login with sessions (certain users are capables of seeing some faqs collapsiles, that's the app, a faq website). Whenever i try to log-in with some admin user, it refuses my connection. But, in another page, from the same app, i fetched all the faqs into tables and it returns succesfully, so the conection with the database is working. but the code for log-in doesnt. any suggestion? :/
Here's the code(login):
<?php
include 'config.php';
session_start();
error_reporting(0);
if (isset($_SESSION['username'])) {
header("Location: welcome.php");
}
if (isset($_POST['submit'])) {
$email = $_POST['email'];
$password = md5($_POST['password']);
$sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
$stmt = sqlsrv_query($conn, $sql);
if ($stmt->num_rows > 0) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$_SESSION['username'] = $row['username'];
header("Location: welcome.php");
} else {
echo "Conexion fallida";
die( print_r(sqlsrv_errors(), true));
}
}
I'm migrating an app from MySql to SQL Server. The thing is that im trying to make the login with sessions (certain users are capables of seeing some faqs collapsiles, that's the app, a faq website). Whenever i try to log-in with some admin user, it refuses my connection. But, in another page, from the same app, i fetched all the faqs into tables and it returns succesfully, so the conection with the database is working. but the code for log-in doesnt. any suggestion? :/
Here's the code(login):
<?php
include 'config.php';
session_start();
error_reporting(0);
if (isset($_SESSION['username'])) {
header("Location: welcome.php");
}
if (isset($_POST['submit'])) {
$email = $_POST['email'];
$password = md5($_POST['password']);
$sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
$stmt = sqlsrv_query($conn, $sql);
if ($stmt->num_rows > 0) {
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$_SESSION['username'] = $row['username'];
header("Location: welcome.php");
} else {
echo "Conexion fallida";
die( print_r(sqlsrv_errors(), true));
}
}
this is the config.php code:
<?php
$dbServer= "myIp\MSSQLSERVER,1433";
$dbName = "myDb";
$dbUser = "myUser";
$dbPassword = "myPass";
$connectionInfo = array(
"Database" => $dbName,
"UID" => $dbUser,
"PWD" => $dbPassword
);
$conn = sqlsrv_connect($dbServer, $connectionInfo);
?>
And, just in case, here's the page that's actually working (without the login system, just fetch all faqs for make sure that the conecction was ok):
<?php
// connect with database
$conn = new PDO("sqlsrv:server=myserver\MSSQLSERVER,1433;Database=faqs;ConnectionPooling=0", "websis", "websis2022");
// fetch all FAQs from database
$showInfo = htmlentities($_SESSION['username'],ENT_QUOTES,'utf-8');
$sql = "SELECT * FROM faqs WHERE id = 4";
$statement = $conn->prepare($sql);
$statement->execute();
$faqs = $statement->fetchAll();
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
