Skip to content

Commit

Permalink
JSON, Session, Cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
hasinhayder committed Nov 3, 2024
0 parents commit d9ae8e3
Show file tree
Hide file tree
Showing 25 changed files with 1,160 additions and 0 deletions.
11 changes: 11 additions & 0 deletions c1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
$person = [
'name' => 'John Doe',
'age' => 30,
'hobbies' => ['Tennis', 'Swimming'],
];

// echo serialize($person);

$newArray = unserialize('a:3:{s:4:"name";s:8:"John Doe";s:3:"age";i:30;s:7:"hobbies";a:2:{i:0;s:6:"Tennis";i:1;s:8:"Swimming";}}');
echo $newArray['name'];
9 changes: 9 additions & 0 deletions c2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
$person = [
'name' => 'John Doe',
'age' => 30,
'hobbies' => ['Tennis', 'Swimming'],
];
$json = json_encode($person,JSON_PRETTY_PRINT);
file_put_contents("person.json", $json);

11 changes: 11 additions & 0 deletions c3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
$jsonData = file_get_contents("person.json");
// $person = json_decode($jsonData);
// var_dump($person);
// print_r($person);
// echo $person->name;

$person = json_decode($jsonData, true);
// var_dump($person);
echo $person['name'];
print_r($person);
53 changes: 53 additions & 0 deletions c4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
$personProfile = [
'name' => 'John Doe',
'age' => 30,
'address' => [
'street' => '123 Main St',
'city' => 'Anytown',
'state' => 'CA',
'zipcode' => '12345'
],
'addresses'=>[
'home'=>[
'street' => '123 Main St X',
'city' => 'Anytown',
'state' => 'CA',
'zipcode' => '12345'
],
'work'=>[
'street' => '456 Business St',
'city' => 'Metro City',
'state' => 'CA',
'zipcode' => '54321'
]
],
'contact' => [
'email' => 'johndoe@example.com',
'phone' => '555-1234'
],
'employment' => [
'job_title' => 'Software Engineer',
'company' => 'Tech Solutions',
'years_experience' => 8
],
'education' => [
'degree' => 'Bachelor of Science in Computer Science',
'university' => 'State University',
'graduation_year' => 2015
],
'skills' => [
'programming_languages' => ['PHP', 'JavaScript', 'Python'],
'frameworks' => ['Laravel', 'React', 'Django'],
'tools' => ['Git', 'Docker', 'Jenkins']
],
'hobbies' => ['reading', 'hiking', 'gaming']
];

// $json = json_encode($personProfile, JSON_PRETTY_PRINT);
// $json = json_encode($personProfile);
// file_put_contents("profile.json", $json);

$personData = file_get_contents("profile.json");
$person = json_decode($personData, true);
echo $person['addresses']['home']['street'];
17 changes: 17 additions & 0 deletions c5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
// $username = "jimmy";
// $password = "123456";

$username = "janedoe";
$password = "password456";

$userData = file_get_contents("users.json");
$users = json_decode($userData, true);

foreach($users as $u=>$p){
if($u==$username && $p==$password){
echo "Login successful";
exit;
}
}
echo "Login failed";
4 changes: 4 additions & 0 deletions c6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
setcookie("fruit", "Mango", time() + 60);

echo "Cookie is set";
8 changes: 8 additions & 0 deletions c7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
$fruit = $_COOKIE['fruit']??null;

if ($fruit){
echo "Your fav fruit is {$fruit}";
}else{
echo "No fav fruit set";
}
203 changes: 203 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php
session_start();
if(!isset($_SESSION['user'])){
header("location:login.php");
}

$user = $_SESSION['user'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Dashboard</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<style>
@media (max-width: 1023px) {
.sidebar {
transform: translateX(-100%);
}

.sidebar.show {
transform: translateX(0);
}
}
</style>
</head>

<body class="bg-gray-100">
<div class="flex h-screen">
<!-- Sidebar -->
<aside id="sidebar" class="fixed inset-y-0 left-0 z-50 w-64 bg-white shadow-lg transform transition-transform duration-300 ease-in-out lg:translate-x-0 lg:static lg:inset-0 sidebar">
<div class="flex items-center justify-between h-16 px-6 bg-blue-600 text-white">
<span class="text-2xl font-semibold">MyApp</span>
<button id="closeSidebar" class="lg:hidden">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<nav class="mt-8">
<ul>
<li>
<a href="#" class="flex items-center px-6 py-3 text-gray-700 hover:bg-gray-100">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6" />
</svg>
<span class="mx-3">Dashboard</span>
</a>
</li>
<li>
<a href="#" class="flex items-center px-6 py-3 text-gray-700 hover:bg-gray-100">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" />
</svg>
<span class="mx-3">Projects</span>
</a>
</li>
<li>
<a href="#" class="flex items-center px-6 py-3 text-gray-700 hover:bg-gray-100">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01" />
</svg>
<span class="mx-3">Tasks</span>
</a>
</li>
<li>
<a href="#" class="flex items-center px-6 py-3 text-gray-700 hover:bg-gray-100">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
<span class="mx-3">Settings</span>
</a>
</li>
</ul>
</nav>
</aside>

<!-- Main Content -->
<div class="flex-1 flex flex-col overflow-hidden">
<!-- Header -->
<header class="flex items-center justify-between px-6 py-4 bg-white border-b">
<div class="flex items-center">
<button id="openSidebar" class="text-gray-500 focus:outline-none focus:text-gray-700 lg:hidden">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
</svg>
</button>
<h1 class="text-2xl font-semibold text-gray-800 ml-4">Dashboard</h1>
</div>
<div class="flex items-center">
<button class="text-gray-500 focus:outline-none focus:text-gray-700 mr-4">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
</svg>
</button>
<button class="text-gray-500 focus:outline-none focus:text-gray-700">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1" />
</svg>
</button>
<img class="h-8 w-8 rounded-full ml-4" src="https://via.placeholder.com/40" alt="User avatar">
</div>
</header>

<!-- Main Content -->
<main class="flex-1 overflow-x-hidden overflow-y-auto bg-gray-100">
<div class="container mx-auto px-6 py-8">
<h2 class="text-3xl font-semibold text-gray-800 mb-6">Welcome back, <?php echo $user;?>!</h2>

<div class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
<div class="bg-white shadow rounded-lg p-4">
<div class="flex items-center">
<div class="p-3 rounded-full bg-indigo-600 bg-opacity-75">
<svg class="h-8 w-8 text-white" viewBox="0 0 28 30" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M18.2 9.08889C18.2 11.5373 16.3196 13.5222 14 13.5222C11.6804 13.5222 9.79999 11.5373 9.79999 9.08889C9.79999 6.64043 11.6804 4.65556 14 4.65556C16.3196 4.65556 18.2 6.64043 18.2 9.08889Z" fill="currentColor" />
<path d="M25.2 12.0444C25.2 13.6768 23.9464 15 22.4 15C20.8536 15 19.6 13.6768 19.6 12.0444C19.6 10.4121 20.8536 9.08889 22.4 9.08889C23.9464 9.08889 25.2 10.4121 25.2 12.0444Z" fill="currentColor" />
<path d="M19.6 22.3889C19.6 19.1243 17.0927 16.4778 14 16.4778C10.9072 16.4778 8.39999 19.1243 8.39999 22.3889V26.8222H19.6V22.3889Z" fill="currentColor" />
<path d="M8.39999 12.0444C8.39999 13.6768 7.14639 15 5.59999 15C4.05359 15 2.79999 13.6768 2.79999 12.0444C2.79999 10.4121 4.05359 9.08889 5.59999 9.08889C7.14639 9.08889 8.39999 10.4121 8.39999 12.0444Z" fill="currentColor" />
<path d="M22.4 26.8222V22.3889C22.4 20.8312 22.0195 19.3671 21.351 18.0949C21.6863 18.0039 22.0378 17.9556 22.4 17.9556C24.7197 17.9556 26.6 19.9404 26.6 22.3889V26.8222H22.4Z" fill="currentColor" />
<path d="M6.64896 18.0949C5.98058 19.3671 5.59999 20.8312 5.59999 22.3889V26.8222H1.39999V22.3889C1.39999 19.9404 3.2804 17.9556 5.59999 17.9556C5.96219 17.9556 6.31367 18.0039 6.64896 18.0949Z" fill="currentColor" />
</svg>
</div>
<div class="mx-5">
<h4 class="text-2xl font-semibold text-gray-700">12</h4>
<div class="text-gray-500">Projects</div>
</div>
</div>
</div>
<div class="bg-white shadow rounded-lg p-4">
<div class="flex items-center">
<div class="p-3 rounded-full bg-green-600 bg-opacity-75">
<svg class="h-8 w-8 text-white" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M4.19999 1.4C3.4268 1.4 2.79999 2.02681 2.79999 2.8C2.79999 3.57319 3.4268 4.2 4.19999 4.2H5.9069L6.33468 5.91114C6.33917 5.93092 6.34409 5.95055 6.34941 5.97001L8.24953 13.5705L6.99992 14.8201C5.23602 16.584 6.48528 19.6 8.97981 19.6H21C21.7731 19.6 22.4 18.9732 22.4 18.2C22.4 17.4268 21.7731 16.8 21 16.8H8.97983L10.3798 15.4H19.6C20.1303 15.4 20.615 15.1004 20.8521 14.6261L25.0521 6.22609C25.2691 5.79212 25.246 5.27673 24.991 4.86398C24.7357 4.45123 24.2852 4.2 23.8 4.2H8.79308L8.35818 2.46044C8.20238 1.83722 7.64241 1.4 6.99999 1.4H4.19999Z" fill="currentColor" />
<path d="M22.4 23.1C22.4 24.2598 21.4598 25.2 20.3 25.2C19.1403 25.2 18.2 24.2598 18.2 23.1C18.2 21.9402 19.1403 21 20.3 21C21.4598 21 22.4 21.9402 22.4 23.1Z" fill="currentColor" />
<path d="M9.1 25.2C10.2598 25.2 11.2 24.2598 11.2 23.1C11.2 21.9402 10.2598 21 9.1 21C7.9402 21 7 21.9402 7 23.1C7 24.2598 7.9402 25.2 9.1 25.2Z" fill="currentColor" />
</svg>
</div>
<div class="mx-5">
<h4 class="text-2xl font-semibold text-gray-700">24</h4>
<div class="text-gray-500">Tasks</div>
</div>
</div>
</div>
<div class="bg-white shadow rounded-lg p-4">
<div class="flex items-center">
<div class="p-3 rounded-full bg-blue-600 bg-opacity-75">
<svg class="h-8 w-8 text-white" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6.99998 11.2H21L22.4 23.8H5.59998L6.99998 11.2Z" fill="currentColor" stroke="currentColor" stroke-width="2" stroke-linejoin="round" />
<path d="M9.79999 8.4C9.79999 6.08041 11.6804 4.2 14 4.2C16.3196 4.2 18.2 6.08041 18.2 8.4V12.6C18.2 14.9197 16.3196 16.8 14 16.8C11.6804 16.8 9.79999 14.9197 9.79999 12.6V8.4Z" stroke="currentColor" stroke-width="2" />
</svg>
</div>
<div class="mx-5">
<h4 class="text-2xl font-semibold text-gray-700">89%</h4>
<div class="text-gray-500">Completed</div>
</div>
</div>
</div>
</div>

<div class="bg-white shadow rounded-lg p-6">
<h3 class="text-xl font-semibold text-gray-800 mb-4">Profile Information</h3>
<form method="POST">
<div class="grid gap-6 mb-6 md:grid-cols-2">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Full Name</label>
<input type="text" value="Jane Doe" readonly class="w-full px-3 py-2 text-gray-700 border rounded-lg focus:outline-none">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Email Address</label>
<input type="email" value="jane.doe@example.com" readonly class="w-full px-3 py-2 text-gray-700 border rounded-lg focus:outline-none">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Role</label>
<input type="text" value="Software Engineer" readonly class="w-full px-3 py-2 text-gray-700 border rounded-lg focus:outline-none">
</div>
</div>
<button class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">Update Profile</button>
</form>
</div>
</div>
</main>
</div>
</div>

<script>
const sidebar = document.getElementById('sidebar');
const openSidebarBtn = document.getElementById('openSidebar');
const closeSidebarBtn = document.getElementById('closeSidebar');

openSidebarBtn.addEventListener('click', () => {
sidebar.classList.add('show');
});

closeSidebarBtn.addEventListener('click', () => {
sidebar.classList.remove('show');
});
</script>
</body>

</html>
7 changes: 7 additions & 0 deletions jsonexample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const person = {
"name": "John",
"age": 30,
"hobbies": ["reading", "travelling"]
}

console.log(person.hobbies[1]); // John
Loading

0 comments on commit d9ae8e3

Please sign in to comment.