PHP login and session get data from databse
PHP login and session get data from databse
This is my code in login. After login i want to userid as primary key to pass another page. i try many code but still not working. please help me
if(isset($_POST['login']))
$email=$_POST['email'];
$password=md5($_POST['password']);
$sql ="SELECT * FROM tbluser WHERE EmailAddress=:email and uPassword=:password";
$query= $dbh -> prepare($sql);
$query-> bindParam(':email', $email, PDO::PARAM_STR);
$query-> bindParam(':password', $password, PDO::PARAM_STR);
$query-> execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() > 0)
$_SESSION['login']=$_POST['email'];
$_SESSION['fname']=$results->FullName;
$_SESSION['uid']=$results['UserID'];
$currentpage=$_SERVER['REQUEST_URI'];
echo "<script type='text/javascript'> document.location = '$currentpage'; </script>";
else
echo "<script>alert('Invalid Details');</script>";
And how to call the userid in another page.
md5()
Don't use JavaScript or Meta Tags to redirect a web page. Use
Location: header
instead. Check PHP the Right Way for more up-to-date advice.– John Conde
Aug 29 at 18:13
Location: header
Are you sure
$currentpage
is set and has a correct value?– John Conde
Aug 29 at 18:13
$currentpage
OK, Thank for recommendation .
$currentspage
for call current page viewwer– Muhammad Wazexr
Aug 29 at 18:25
$currentspage
2 Answers
2
you need session_start()
session_start()
if($query->rowCount() > 0)
session_start();
$_SESSION['login']=$_POST['email'];
$_SESSION['fname']=$results->FullName;
$_SESSION['uid']=$results['UserID'];
$currentpage=$_SERVER['REQUEST_URI'];
echo "<script type='text/javascript'> document.location = '$currentpage'; </script>";
and then you start the session with session_start() on the other page and it will work.
This code in another page or in login page?
– Muhammad Wazexr
Aug 29 at 18:25
ALL page need a session_start() to use session
– Dice
Aug 29 at 18:26
Put it at beginning of everyphp file. I show you.
– Dice
Aug 29 at 18:26
@MuhammadWazexr use session_start() on top of every page,that way $_SESSION variables will be stored
– Ruben Perdigao
Aug 29 at 18:27
@Dice Ok sir. but this code correct?
$_SESSION['uid']=$results['UserID'];
– Muhammad Wazexr
Aug 29 at 18:30
$_SESSION['uid']=$results['UserID'];
That's how you use a session, put it at beginning or every php file where you need it
<?php
session_start();
include("./Models/db_connect.php");
include('./Controllers/Functions/PHP/messages.php');
include('./Models/actual_date.php');
$actual_date = get_date($db);
switch(isset($_POST['login'])):
case 'Register':
$email = htmlspecialchars(trim($_POST['em']), ENT_QUOTES, 'UTF-8');
$password = htmlspecialchars(trim($_POST['pw']), ENT_QUOTES, 'UTF-8');
// check if the combination fname/lname/email is already used
include('./Models/log_check.php');
unset($_SESSION['ID'],$_SESSION['role']);
$_SESSION['ID'] = $row['ID'];
$_SESSION['role'] = $row['role'];
if(intval($row['ID']) > 0)
include('./Models/status_update.php');
$successmsg = "Connexion réussie! Redirection en cours";
header('refresh:5;url=./index.php?page=Lobby');
if($_SESSION['role'] === 'vet')
include './Views/html_top_vets.php';
else
include './Views/html_top_clients.php';
include('./Views/lobby.php');
else
$errormsg = "
<p>Vous n'avez pas de compte ou la combinaison est incorrecte!</p>";
include('./Views/html_top_login.php');
include('./Views/login.php');
break;
default:
include('./Views/html_top_login.php');
include('./Views/login.php');
endswitch;
?>
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
md5()
is obsolete for hashing passwords and should not be used. PHP provides password_hash() and password_verify(), please use them. And here are some good ideas about passwords. If you are using a PHP version prior to 5.5 there is a compatibility pack available here.– John Conde
Aug 29 at 18:12