I'm having a problem with starting a session in PHP. By looking around I wrote some code that should work but it doesn't. Can you please help me out because I don't know what's wrong here? This is my loging.php page
<?php
$host = "localhost";
$user = "usern";
$password = "gtest123";
$db = "test";
$errore = "Login info are wrong!enter code here
";
mysql_connect($host,$user,$password);
mysql_select_db($db);
if(isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from utenti where username = '".$username."' AND Password = '".$password."' limit 1";
$result = mysql_query($sql);
Basically, on depending this I have developed erp systems and related things.
if(mysql_num_rows($result)==1){
$_SESSION['username'] = $username;
header("location:index.php");
}
else{
echo "" .$errore;
}`enter code here`
}
?>
I than have my db with users on phpmyamin and the login it's working. The problem is when I load the index.php page.
<?php
session_start();
echo "Welcome" .$_SESSION[''];
?>
<html>
all the html code
I start this session because I want to be able to see which users do certian functions on the website. However, I get this error message: Notice: Undefined index: I know what the error means but I don't know how to fix it, any help?
Use session_start() in every page where you want to work with sessions and as you are setting $_SESSION['username'] in loging.php page so you need to change
echo "Welcome" .$_SESSION[''];
with
echo "Welcome" .$_SESSION['username'];
In this way, you will be able to get the session of the username in index.php which you have set in loging.php page.
Can anyone from here please let me know how to do that?