Маринакон
0 / 0 / 0
Регистрация: 08.11.2016
Сообщений: 27
|
|
1 | |
Php mysql04.12.2019, 21:23. Просмотров 80. Ответов 0
Помогите пожалуйста по коду сформировать бд в phpadmin и таблицу, в которой данные пользователя:
<?php error_reporting(E_ALL); session_start(); console_log($_SESSION); check_aut(); if(isset($_COOKIE['key'])) { $key=$_COOKIE['key']; $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $zap = "select user_id from authorization where aut_key = '$key'"; if ($result = $bd->query($zap)) { if ($result->num_rows) { $ar=$result->fetch_assoc(); $_SESSION['id_user'] = $ar['user_id']; $zap = "select user_name from user where id = '${ar['user_id']}'"; $result = $bd->query($zap); $ar=$result->fetch_assoc(); $_SESSION['user_name'] = $ar['user_name']; } } } function console_log( $data ) { echo '<script>'; echo 'console.log('. json_encode( $data ) .')'; echo '</script>'; } function print_js( $data ) { echo '<script>'; echo 'print_ex('. json_encode( $data ) . ');'; echo '</script>'; } function gen_key() { $alf = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $len = strlen($alf); $rez = ""; for ($i = 0; $i < 50; ++$i) $rez .= $alf[random_int(0, $len - 1)]; return $rez; } function autorization($id, $bd, $user_name) { $key=gen_key(); $zap = "insert into authorization (aut_key, user_id) values ('$key', '$id')"; $bd->query($zap); setcookie("key", $key, time() + 60 * 60 * 24 * 365); $_SESSION["id_user"] = $id; $_SESSION["user_name"] = $user_name; } if (isset($_POST["sent-log"])) { $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $user_name = $_POST["login"]; $password = $_POST["password"]; $zap = "select id, user_name from user where user_name = '$user_name' and password='$password'"; if ($result = $bd->query($zap)) { if ($result->num_rows) { console_log($result); $ar=$result->fetch_assoc(); autorization($ar['id'], $bd, $ar['user_name']); $bd->close(); header("location: profile.php"); exit; } else { $bd->close(); print_js('Вход не осуществлен, неверные данные! Попробуйте снова!'); } } else { $bd->close(); print_js('Вход не осуществлен, неверные данные! Попробуйте снова!'); } } if (isset($_POST["sent_ext"])) { if (isset($_COOKIE["key"]) || isset($_SESSION["id_user"])) { $key = $_COOKIE["key"]; $id_user = $_SESSION["id_user"]; setcookie("key", "", 1); unset($_SESSION["id_user"]); $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $zap = "delete from authorization where aut_key = '$key' or user_id = '$id_user'"; $bd->query($zap); $bd->close(); header("location: index.php"); exit; } } function check_aut() { if (!isset($_SESSION["id_user"]) && isset($_COOKIE["key"])) { $key = $_COOKIE["key"]; $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $zap = "select user_id from authorization where aut_key='$key'"; if ($result = $bd->query($zap)) { if ($arr = $result->fetch_assoc()) $_SESSION["id_user"] = $arr["user_id"]; } $bd->close(); } } console_log($_POST); $uploadfile=''; if(isset($_POST["change_reg"])) { $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $id_user = $_SESSION["id_user"]; $fname = $_POST["fname"]; $sname = $_POST["sname"]; $oname = $_POST["oname"]; $email = $_POST["email"]; $zap = "update user SET fname = '$fname', fname = '$fname', oname = '$oname', email='$email' where id=$id_user"; $bd->query($zap); $bd->close(); header("location: profile.php"); exit; } if(isset($_POST["change_photo"])) { $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); if(isset($_FILES) && $_FILES['avatar']['error'] == 0) { @mkdir("avatars", 0777); $user_name=$_SESSION["user_name"]; $uploaddir = './avatars/'; $namefile=$user_name.".".pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION); $uploadfile = $uploaddir.basename($namefile); console_log($namefile); console_log($uploadfile); if (copy($_FILES['avatar']['tmp_name'], $uploadfile)) { $id_user = $_SESSION["id_user"]; $zap = "update user SET photo = '$uploadfile' where id=$id_user"; $bd->query($zap); } } $bd->close(); header("location: profile.php"); exit; } if (isset($_POST["sent-reg"])) { console_log("qwerty"); $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); console_log($bd); $fname = $_POST["fname"]; $sname = $_POST["sname"]; $oname = $_POST["oname"]; $user_name = $_POST["login"]; $password = $_POST["password"]; $email = $_POST["email"]; console_log($_FILES); if(isset($_FILES) && $_FILES['avatar']['error'] == 0) { @mkdir("avatars", 0777); $uploaddir = './avatars/'; $namefile=$user_name.".".pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION); $uploadfile = $uploaddir.basename($namefile); console_log($namefile); console_log($uploadfile); if (copy($_FILES['avatar']['tmp_name'], $uploadfile)) { echo "<h3>Файл успешно загружен на сервер</h3>"; } else { echo "<h3>Ошибка! Не удалось загрузить файл на сервер!</h3>"; exit; } } // //$rez = $name . ", " . $sname . ", " . $midname . ", " . $login . ", " . $password . ", " . $email; // //console_log($rez); $zap = "insert into user (user_name, password, fname, sname, oname, email, photo) values('$user_name', '$password', '$fname', '$sname','$oname', '$email', '$uploadfile')"; console_log($zap); // $bd->query($qry); if ($result = $bd->query($zap)) { console_log($result); $bd->close(); header("location: index.php"); exit; } else { print_js('Регистрация прошла не успешно! попробуйте снова!'); $bd->close(); } } if (isset($_GET["change_pass"]) && isset($_SESSION["id_user"])) { $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $key = gen_key(); $user_id = $_SESSION["id_user"]; $zap = "update user SET link='$key' where id = $user_id"; $bd->query($zap); print_js("localhost/lab4/change_pass.php?link=" . $key); $bd->close(); } if (isset($_GET["link"])) { $key = $_GET["link"]; //$bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $_SESSION["link"] = $key; } if (isset($_POST["change-pass"])) { $key = $_SESSION["link"]; $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $password = $_POST["password"]; $zap = "update user SET link=NULL, password='$password' where link='$key'"; $bd->query($zap); $bd->close(); unset($_SESSION["link"]); header("location: index.php"); exit; } if (isset($_POST["sent_change_pass"])) { $bd = new mysqli("127.0.0.1", "root", "cvfqkbr", "users"); $key = gen_key(); $user_name = $_POST["login"]; $zap = "update user SET link='$key' where user_name = '$user_name'"; if ($bd->query($zap)) { if ($bd->affected_rows > 0) { print_js("localhost/lab4/change_pass.php?link=" . $key); $bd->close(); //header("location: index.php"); //exit; } else { print_js('Данного пользователя не существует!'); //$bd->close(); } } else { print_js($bd->error); //$bd->close(); } //$bd->query($zap); $bd->close(); } ?>
0
|
|
QA
Эксперт
41792 / 34177 / 6122
Регистрация: 12.04.2006
Сообщений: 57,940
|
04.12.2019, 21:23 |
Ответы с готовыми решениями:
|
Answers
Эксперт
37091 / 29110 / 5898
Регистрация: 17.06.2006
Сообщений: 43,301
|
04.12.2019, 21:23 |
php + mysql MySQL PHP php и mysql Искать еще темы с ответами Или воспользуйтесь поиском по форуму: |