php - Get Username from database after login
Get the solution ↓↓↓I'm trying to login a user first, redirect them to an admin panel and then say something like "Good Morning Tim!".
Now to the problem: I use a session to login the user and the user then gets redirected to the admin panel, so I need to set a cookie with the username and lateron read the cookie on another site.
Here a bit of the code:
** Login-site PHP **
$viewable = true;
require_once( './includes/login.php' );
if( isset( $_POST[/*button*/'login'] ) ) {
if( !empty( $_POST['user'] ) && !empty( $_POST['password'] ) ){
login( $_POST['user'], $_POST['password'], $loginsite );
}
}else if ( isset( $_POST['register'] ) ) {
if( !empty( $_POST['user'] ) && !empty( $_POST['password'] ) ){
register( $_POST['Benutzer'], $_POST['Passwort'] );
}
}
** MySQL PHP ** <?php
require_once('utils.php');
class Connection {
private const server = 'ip';
private const username = 'user';
private const password = 'pw';
private const logindb = 'database';
private $mysqli;
private $debug;
function __construct( $debug = false )
{
$this->mysqli = new mysqli( self::server, self::username,self::password, 'database' );
$this->connect();
$this->createTableIfNoExists();
$this->debug = $debug;
}
private function connect(){
if ( $this->mysqli->connect_error ) die( "That didn't work! Contact us." .$this->mysqli-
>connect_error );
}
private function createTableIfNoExists() {
//creates database
$logindb
$sql = "CREATE TABLE IF NOT EXISTS " . self::logindb . "(
id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
password VARCHAR(1024) NOT NULL,
salt VARCHAR(128) NOT NULL,
PRIMARY KEY(id)
);";
$this->mysqli->query( $sql );
}
//debug
private function debug( $msg ){
if( $this->debug ){
debug_to_console( $msg );
}
}
//testet ob die anmeldung erfolgreich war
public function mysql_checkLogin( $name, $pw ){
$this->debug( 'Checking Login of ' . $name . ' :: ' . $pw );
$stmt = $this->mysqli->prepare( "SELECT name, password, salt FROM " . self::logindb . " WHERE
name = ?" );
$stmt->bind_param( "s", $name );
$stmt->execute();
$stmt->store_result();
if( $stmt->num_rows === 0 )
{
$stmt->close();
$this->debug( 'Could not login' );
return false;
}
$stmt->bind_result( $uname, $password, $salt );
$stmt->fetch();
$stmt->close();
$enc = md5( $pw . $salt );
$this->debug( 'Testing Hash' );
$this->debug( 'Salt is ' . $salt );
$this->debug( 'From Database: ' . $password );
$this->debug( 'Encrypted : ' . $enc );
if( $enc === $password && $uname === $name ) return true;
return false;
}
And the admin panel:
<?php $erg = mysqli_query($db, "SELECT name FROM database");
while($row = mysqli_fetch_object($erg)){
$fullname=$row->name;
$names = explode(" ",$fullname);
echo $names[0];}
Answer
Solution:
I got it to work with a cookie.
admin panel:
$cookie_name = "user";
$cookie_value = $uname;
setcookie($cookie_name, $cookie_value, time()+3600, "/", "domain.de/panel/", 1);
$fullname=$_COOKIE[$cookie_name];
$names = explode(" ",$fullname);
echo $names[0];
And I implemented
password_hash()
Thanks to Dharman!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreign key constraint is incorrectly formed laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.