-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariablesSesion.php
53 lines (50 loc) · 1.96 KB
/
variablesSesion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
session_start();
// Conexion con la base de datos
// header("Content-type:text/html;charset=utf-8");
//echo"Usuario con sesion iniciada".$_SESSION['nombre'];
if (array_key_exists('nombre',$_POST) OR array_key_exists('email',$_POST))
{
$enlace = mysqli_connect("localhost", "root", "","contacto");
if (mysqli_connect_error()) {
die("Hubo un error en la conexión, inténtelo más tarde");
}
if ($_POST['nombre']=='')
{
echo "<p>El nombre de usuario es obligatorio</p>";
}
else if ($_POST['email']=='')
{
echo "<p>La contraseña es obligatoria</p>";
}
else
{
// El usuario ha rellenado ambos campos
$query = "SELECT nombre FROM datos WHERE nombre='".mysqli_real_escape_string($enlace,$_POST['nombre'])."'";
$result = mysqli_query($enlace,$query);
if (mysqli_num_rows($result)>0)
{
echo "<p>Ese nombre de usuario ya está registrado. Intenta con otro</p>";
}
else
{
// Añadir a nuestro usuario a la BD
$query="INSERT INTO datos (nombre, email) VALUES('".mysqli_real_escape_string($enlace,$_POST['nombre'])."','".mysqli_real_escape_string($enlace,$_POST['email'])."')";
if (mysqli_query($enlace,$query)){
$_SESSION['nombre']=$_POST['nombre'];
// echo "<p>¡Enhorabuena! Has registrado tu cuenta</p>";
}
else
{
echo "<p>Hubo algún problema al registrar el usuario. Inténtelo más tarde</p>";
}
}
}
}
?>
<h1>Registro de usuario</h1>
<form method="POST">
<input type="text" name="nombre" placeholder="Escribe tu nombre de usuario">
<input type ="email" name="email">
<input type="submit" value="Registrar">
</form>