index.php
Открыть спойлер
Закрыть спойлер
- <?php
- include('chek_coockie.php');
- ?>
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Что-то не понятное</title>
- <link rel="stylesheet" type="text/css" href="style.css" />
- </head>
- <body>
- <div id="container">
- <div id="header" >Шапка сайта
- <div id="form" style="text-align:center;float:right">
- <?php
- include('./Login_or_unlogin_form.php')
- ?>
- </div>
- </div>
- <div id="navigation"></div>
- <div id="menu" >Зарегистрированные:<br>
- <hr style="border-style:solid;border-width:1px;"></div>
- <div id="content"></div>
- <div id="clear"> </div>
- </div>
- <div id="footer" style="text-align:center;font-size:15px;"></div>
- </body>
- </html>
chek_coockie.php
Открыть спойлер
Закрыть спойлер
- <?php
- if(isset($_COOKIE['id'])and isset($_COOKIE['hash']))
- {
- $link=mysqli_connect('localhost', 'root','','');
- $query = mysqli_query($link, "SELECT * FROM Users WHERE id = '".intval($_COOKIE['id'])."'");
- $userdata = mysqli_fetch_assoc($query);
- if(($userdata['hash'] != $_COOKIE['hash'])or ($userdata['id'] != $_COOKIE['id']))
- {
- setcookie('id','');
- setcookie('name','');
- setcookie('hash','');
- }else{
- if(isset($_POST['Exit']))
- {
- setcookie('id','');
- setcookie('name','');
- setcookie('hash','');
- }
- }
- }
- if(isset($_POST['sumbit']))
- {
- $link=mysqli_connect('localhost', 'root','','');
- $query = mysqli_query($link,"SELECT * FROM users WHERE Name='".mysqli_real_escape_string($link,$_POST['name'])."'");
- $userdata = mysqli_fetch_assoc($query);
- function generateCode($length=6) {
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHI JKLMNOPRQSTUVWXYZ0123456789";
- $code = "";
- $clen = strlen($chars) - 1;
- while (strlen($code) < $length) {
- $code .= $chars[mt_rand(0,$clen)];
- }
- return $code;}
- if($userdata['Password']==($_POST['pass']))
- {
- $hash = md5(generateCode(10));
- mysqli_query($link, "UPDATE users SET hash='".$hash."'WHERE id='".$userdata['id']."'");
- setcookie('id',$userdata['id'],time()+3600,"/");
- setcookie('name',$userdata['Name'],time()+3600,"/");
- setcookie('hash',$has,time()+3600,"/");
- }
- }
Login_or_unlogin_form.php
Открыть спойлер
Закрыть спойлер
- <?php
- if(!isset($_COOKIE['name']))
- {
- echo('<form method="POST">
- <p style="line-height:0.2;text-align:left">Имя:<input style="float:right; margin-top:-0.6em;" type="text" name="name" /></p>
- <p style="line-height:0.2;text-align:left">Пароль:<input style="float:right;margin-top:-0.6em;" type="password" name="pass" /></p>
- <input style="margin-left:auto" name="sumbit" type="submit" value="Войти" /> <a style="font-size:15px;" href="register_page.php">Зарегистрироваться ?</a>
- </form>');
- }
- else{
- echo('<p class="style2" align="center">Привет, '.$_COOKIE['name'].' </p>');
- echo('<form method="POST">
- <input class="button" name="Exit" type="submit" value="Выход">
- </form>');
- }