Skip to content

Instantly share code, notes, and snippets.

@deuterium7
Last active August 8, 2017 12:05
Show Gist options
  • Select an option

  • Save deuterium7/70d733d0d25ed34e33d324bb174a4285 to your computer and use it in GitHub Desktop.

Select an option

Save deuterium7/70d733d0d25ed34e33d324bb174a4285 to your computer and use it in GitHub Desktop.
Zabornyi Alex

Работа с сессиями

0 - подготовка

Session.php

<?php
	class Session
	{
		public function __construct() {
			session_start();
		}

		public function saveValue($key, $value) {

			if ( !isset($_SESSION[$key]) ) {
				$_SESSION[$key] = $value;	
			}
		}

		public function deleteValue($key) {

			if ( isset($_SESSION[$key]) ) {
				unset($_SESSION[$key]);
			}
		}

		public function updateValue($key, $value) {

			if ( isset($_SESSION[$key]) ) {
				$_SESSION[$key] = $value;	
			}
		}

		public function getValue($key) {

			if ( isset($_SESSION[$key]) ) {
				return $_SESSION[$key];
			}
		}

		public function destroySession() {
			
			if ( isset($_SESSION) ) {
				session_destroy();
			}
		}
	}
?>

1

index.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	$session->saveValue('country', $_REQUEST['country']);
?>

<form action="" method="get">
	<p>Страна <input type="text" name="country"></p>
	<p><input type="submit"></p>
	<p><a href="test.php">test.php</a></p>
</form>

test.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	echo $session->getValue('country');

	echo "<br><br><a href='index.php'>index.php</a>";
?>

2

index.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	$session->saveValue('country', $_REQUEST['country']);
	$session->saveValue('login', time());
?>

<form action="" method="get">
	<p>Страна <input type="text" name="country"></p>
	<p><input type="submit"></p>
	<p><a href="test.php">test.php</a></p>
</form>

test.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	echo $session->getValue('country')."<br>";
	echo (time() - $session->getValue('login'))."<br>";

	echo "<br><br><a href='index.php'>index.php</a>";
?>

3

index.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	$session->saveValue('country', $_REQUEST['country']);
	$session->saveValue('login', time());
	$session->saveValue('email', $_REQUEST['email']);
?>

<form action="" method="get">
	<p>Страна <input type="text" name="country"></p>
	<p>Email <input type="text" name="email"></p>
	<p><input type="submit"></p>
	<p><a href="test.php">test.php</a></p>
</form>

test.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	echo $session->getValue('country')."<br>";
	echo (time() - $session->getValue('login'))."<br>";
?>

<form action="" method="get">
	<p>Имя <input type="text" name="name"></p>
	<p>Фамилия <input type="text" name="surname"></p>
	<p>Пароль <input type="text" name="password"></p>
	<p>Email <input type="text" name="email" value="<?=$session->getValue('email')?>"></p>
	<p><input type="submit"></p>
	<p><a href="index.php">index.php</a></p>
</form>

4

index.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	$session->saveValue('country', $_REQUEST['country']);
	$session->saveValue('login', time());
	$session->saveValue('email', $_REQUEST['email']);

	$session->saveValue('count', 0);
	$session->updateValue('count', $session->getValue('count') + 1);

	if ( $session->getValue('count') == 1 ) {
		echo "Первый заход на страницу";
	} else {
		echo $session->getValue('count');	
	}
?>

<form action="" method="get">
	<p>Страна <input type="text" name="country"></p>
	<p>Email <input type="text" name="email"></p>
	<p><input type="submit"></p>
	<p><a href="test.php">test.php</a></p>
</form>

5

index.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	$session->saveValue('country', $_REQUEST['country']);
	$session->saveValue('login', time());
	$session->saveValue('email', $_REQUEST['email']);

	$session->saveValue('count', 0);
	$session->updateValue('count', $session->getValue('count') + 1);

	if ( $session->getValue('count') == 1 ) {
		echo "Первый заход на страницу";
	} else {
		echo $session->getValue('count');
	}

	$session->saveValue('town', $_REQUEST['town']);
	$session->saveValue('age', $_REQUEST['age']);
?>

<form action="" method="get">
	<p>Страна <input type="text" name="country"></p>
	<p>Город <input type="text" name="town"></p>
	<p>Возраст <input type="text" name="age"></p>
	<p>Email <input type="text" name="email"></p>
	<p><input type="submit"></p>
	<p><a href="test.php">test.php</a></p>
</form>

test.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	echo $session->getValue('country')."<br>";
	echo (time() - $session->getValue('login'))."<br>";
?>

<form action="" method="get">
	<p>Имя <input type="text" name="name"></p>
	<p>Фамилия <input type="text" name="surname"></p>
	<p>Пароль <input type="text" name="password"></p>
	<p>Город <input type="text" name="town" value="<?=$session->getValue('town')?>"></p>
	<p>Возраст <input type="text" name="age" value="<?=$session->getValue('age')?>"></p>
	<p>Email <input type="text" name="email" value="<?=$session->getValue('email')?>"></p>
	<p><input type="submit"></p>
	<p><a href="index.php">index.php</a></p>
</form>

6

index.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	$session->saveValue('country', $_REQUEST['country']);
	$session->saveValue('login', time());
	$session->saveValue('email', $_REQUEST['email']);

	$session->saveValue('count', 0);
	$session->updateValue('count', $session->getValue('count') + 1);

	if ( $session->getValue('count') == 1 ) {
		echo "Первый заход на страницу";
	} else {
		echo $session->getValue('count');
	}

	$session->saveValue('town', $_REQUEST['town']);
	$session->saveValue('age', $_REQUEST['age']);
?>

<form action="" method="get">
	<p>Страна <input type="text" name="country"></p>
	<p>Город <input type="text" name="town"></p>
	<p>Возраст <input type="text" name="age"></p>
	<p>Email <input type="text" name="email"></p>
	<p><input type="submit"></p>
	<p><a href="test.php">test.php</a></p>
	<p><a href="logout.php">logout.php</a></p>
</form>

logout.php

<?php
	ini_set('display_errors', 1);
	include('Session.php');
	
	$session = new Session();
	$session->destroySession();

	echo "<a href='index.php'>index.php</a>";
?>

Задачи на cookie (куки) в PHP

0 - подготовка

Cookie.php

<?php
	class Cookie
	{
		public function saveValue($key, $value = '', $time = 0, $path = '') {

			if ( !isset($_COOKIE[$key]) ) {
				setcookie($key, $value, $time, $path);
			}
		}

		public function updateValue($key, $value = '', $time = 0, $path = '') {

			if ( isset($_COOKIE[$key]) ) {
				setcookie($key, $value, $time, $path);	
			}
		}

		public function deleteValue($key) {

			if ( isset($_COOKIE[$key]) ) {
				setcookie($key, '', time());
			}
		}

		public function getValue($key) {

			if ( isset($_COOKIE[$key]) ) {
				return $_COOKIE[$key];
			}
		}
	}
?>

1

1.php

<?php
	ini_set('display_errors', 1);
	include('Cookie.php');
	
	$cookie = new Cookie();
	$cookie->saveValue('count', 1);
	$cookie->updateValue('count', $cookie->getValue('count') + 1);
	echo $cookie->getValue('count')."<br>";
?>

2

2.php

<?php
	ini_set('display_errors', 1);
	include('Cookie.php');
	
	$cookie = new Cookie();
	$cookie->saveValue('unshow', $_REQUEST['unshow'], time()+60*60*24*31);
?>

<!DOCTYPE html>
<html>
<head>
	<title>Banner</title>
	<style type="text/css">
		.banner { width: 150px; height: 50px; background-color: yellow; text-align: center; }
	</style>
</head>
<body>
	<?php
		if ( $cookie->getValue('unshow') ) {
			echo "Баннер скрыт!!!";
		} else {
			$html = "<div class='banner'>";
			$html .= "<a href='https://google.com'>Google</a>";
			$html .= "</div>";
			echo $html;
		}
	?>

	<form action="" method="get">
		<input type="hidden" name="unshow" value="true">
		<input type="submit" value="Не показывать баннер">
	</form>
</body>
</html>

3

3.php

<?php
	ini_set('display_errors', 1);
	include('Cookie.php');
	
	$cookie = new Cookie();
	$cookie->saveValue('lastVisit', date('d'));

	if ( !empty($cookie->getValue('lastVisit')) ) {
		echo "Вы не были в сети ".(date('d') - $cookie->getValue('lastVisit'))." дней";	
	}
?>

4

4.php

<?php
	ini_set('display_errors', 1);
	include('Cookie.php');
	
	$cookie = new Cookie();
	$cookie->saveValue('day', $_REQUEST['day']);
	$cookie->saveValue('month', $_REQUEST['month']);

	if ( !empty($cookie->getValue('day')) && !empty($cookie->getValue('month')) ) {
		$day = $cookie->getValue('day');
		$month = $cookie->getValue('month');

		if ( $day != date('d') || $month != date('m') ) {
			echo "До дня рождения ".abs($day - date('d'))." дней и ".abs($month - date('m'))."  месяцев";
		} else {
			echo "Поздравляем!!!";
		}
	}
?>

<form action="" method="get">
	<p>День <input type="text" name="day"></p>
	<p>Месяц <input type="text" name="month"></p>
	<p><input type="submit"></p>
</form>

5

5.php

<?php
	ini_set('display_errors', 1);
	include('Cookie.php');
	
	$cookie = new Cookie();
	$cookie->saveValue('color', $_REQUEST['color']);
?>

<!DOCTYPE html>
<html>
<head>
	<title>Header</title>
	<style type="text/css">
		header { height: 100px; background-color: gray; text-align: center; }
		.green { background-color: green; }
		.blue { background-color: blue; }
		.yellow { background-color: yellow; }
	</style>
</head>
<body>
	<header class="<?=$cookie->getValue('color')?>">
		<p>Содержимое</p>
	</header>
	<form action="" method="get">
		<select name="color">
			<option value="green">green</option>
			<option value="blue">blue</option>
			<option value="yellow">yellow</option>
		</select>
		<input type="submit">
	</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment