Can't use php $_SESSION

Username: epiz_28633651

I’ve been trying to do a login system using mysql and php. I wanted to use $_SESSION function, but it didn’t work.

Here are my codes:
girisyap.php

<?php 
session_start();
//some codes to read data from db
$_SESSION['id'] = $row['id'];

admin.php

<?php
session_start();
if(!isset($_SESSION['id'])){
echo '<script>'.'alert("Please login");'.'window.location.replace("/giris.php");'.'</script>';
}
?>
//html codes

And it redirects me to giris.php with an alert “Please login” even if I logged in.

  • I checked if there is a problem with db and db connection, but they are working.

  • I tried it from different browser. The result was the same

So, what is the problem here?

The problem with your code is your checking whether $_SESSION['id'] exists or not, so no matter what the value, if it exists, it will return true.

In some cases it makes more sense to use empty() instead of isset() as it then checks whether the value is 0, NULL, or false, and returns false if it is any of those. Try changing isset() to empty() and removing the !. (See code below)

if(empty($_SESSION['id'])){
2 Likes

Tried using empty() but it didn’t work. The result was same.

Ok, may I ask what is being stored as the user id? (Ie what number)

Number from db. Now I’m trying with 2.

1 Like

Ok so just as a note, if the number is 0, empty() will return false.

Just so I can get a bit more context, could you the code your using?

1 Like
<?php 
session_start(); 
include "db_kullanicibaglan.php"; //db connection

if (isset($_POST['username']) && isset($_POST['pass'])) {

	function validate($data){
       $data = trim($data);
	   $data = stripslashes($data);
	   $data = htmlspecialchars($data);
	   return $data;
	}
	$access = $_POST['access'];
	$uname = validate($_POST['username']);
	$pass = validate($_POST['pass']);

	if (empty($uname)) {
		header("Location: giris.php?error=Yanlış/Hatalı Kullanıcı Adı");
	    exit();
	}else if(empty($pass)){
        header("Location: giris.php?error=Yanlış/Hatalı Şifre");
	    exit();
	}else{
		$sql = "SELECT * FROM users WHERE name='$uname' AND sifre='$pass'";

		$result = mysqli_query($conn, $sql);

		if (mysqli_num_rows($result) === 1) {
			$row = mysqli_fetch_assoc($result);
			if ($row['name'] === $uname && $row['sifre'] === $pass) {
                if($row['makam'] === 'admin') {
				$_SESSION['user_name'] = $row['user_name'];
            	$_SESSION['name'] = $row['name'];
            	$_SESSION['id'] = $row['id'];
				$_SESSION['makam'] = $row['makam'];
				$_SESSION['sifre'] = $row['sifre'];
				$_SESSION['foto'] = $row['foto'];
				if(isset($access)){
					header("Location: http://{$access}");
					exit();
				}else {
				header("Location: saybirpank/admin5/admin.php");
				exit();
				}
				}elseif($row['makam'] === 'kullanici') {
				$_SESSION['user_name'] = $row['user_name'];
            	$_SESSION['name'] = $row['name'];
            	$_SESSION['id'] = $row['id'];
				$_SESSION['makam'] = $row['makam'];
				$_SESSION['sifre'] = $row['sifre'];
				$_SESSION['foto'] = $row['foto'];
            	if(isset($access)){
					header("Location: http://{$access}");
				}else {
				header("Location: saybirpank/ogrenci/ogrenci.php");
				}
		        exit();
				}
				else{
				echo '<script>'.'alert("Makam Hatası\nSite Yöneticisiyle İrtibata Geçiniz");'.'window.location.replace("/giris.php");'.'</script>';
		        
				}
            }else{
				echo '<script>'.'alert("Yanlış/Hatalı Kullanıcı Adı veya Şifre");'.'window.location.replace("/giris.php");'.'</script>';
			}
		}else{
			echo '<script>'.'alert("Yanlış/Hatalı Kullanıcı Adı veya Şifre");'.'window.location.replace("/giris.php");'.'</script>';
		}
	}
	
}else{
	echo '<script>'.'alert("Veri Hatası");'.'window.location.replace("/giris.php");'.'</script>';
}

Here is the girisyap.php. Sorry for the language. I’m not english.

its not because of your code. The problem is probably internal because my sessions are not working aswell. You can try to open php error logs from alter php tab in the cPanel you will see an error message like this: " : Warning : session_start(): open(/php_sessions/sess_487075dd161541a5a029402ccfc04144, O_RDWR) failed: No such file or directory (2) in**/home/vol16_1/epizy.com/###** on line3" I am from your country aswell but I can not type in “that” language to obey the rules,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.