Jump to content

Nefferson

New Members
  • Posts

    6
  • Joined

  • Last visited

Posts posted by Nefferson

  1. Hi there. I am actulally working on a login verification process in php. this like my first try i.e i am new on the php code. Ithink i did everything good, but each time i try to run my code by entering real data on the table of my data base, I got a undifined Privilege variable. I sent the code in here so you can give me some help please. the comments on the code are in french, sorry, too much lazy to change them. but I hope you get the logic of the code without the comments. 

     

    <?php Session_start();
    // Start a session on verify page after the verification process
    ?>
    <?php
    // define variables and set empty values
    $nameErr= $emailErr = $passwordErr = "" ;
    $name = $email = $password ="";
    $privilege = "";
    $safename="";
     
    #definition de la function
    function test_input($data)
    {
    $data = trim($data);
    $data = stripcslashes($data);
    $data = strip_tags($data);
    return $data;
    }
    #PROCESSUS DE VERIFICATION
     
    if ($_SERVER["REQUEST_METHOD"]=="POST")# this means if the form has been submited.
     
    { $safename= strip_tags($_POST["Username"]);// On rend inoffensives les balises HTML que le visiteur a pu rentrer
     
    if (filter_var($safename, FILTER_VALIDATE_EMAIL)) {$safename = $email ;}
    else  {$safename = $name ; }# on ferme apres pour eviter confusion
     
    if (empty($_POST["Username"])) { $nameErr="Name is required";} else
    { $name = test_input($_POST["Username"]); // on évite les carateres non alfanumériques et les espaces blancs
    if (!preg_match("/^[a-zA-Z0-9]*$/", $name)) { $nameErr="only letters and white space allowed"; }}
    if (empty($_POST["Password"])) { $passwordErr="Password is required";} else
    { $password = test_input($_POST["Password"]);}       
     
    if ($name != null and preg_match("/^[a-zA-Z0-9]*$/", $name) and  $password != null or
     $email !=null and filter_var($email,FILTER_VALIDATE_EMAIL) and  $password != null )
    { #ici on va crypter le mot de passe
    # $password= Encrypter::encrypt($password);
    $username="root";
    $password_bdd="";
    // On se connecte à MySQL
    try
     
    {
    $bdd = new PDO('mysql:host=localhost;dbname=espacio_estudiantil;charset=utf8',
    $username, $password_bdd, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    }
    catch (Exception $e)
    // En cas d'erreur, on affiche un message et on arrête tout
    {
    die('Erreur : ' . $e->getMessage());
    }
     
     
    $req = $bdd->prepare('SELECT * FROM users_list WHERE Email = :Email and
    Username = :Username and Password = :Password');
    $req->execute(array(
    'Email'  => $email,
    'Password' => $password,
    'Username' => $name));
     
     
    /* On affiche chaque entrée provenant de la variable $req une a une 
    dans la nouvelle variable $données sous forme d'array pour la rendre plus lisible*/
    while ($donnees = $req ->fetch())
     
    {/*there we create a $Privilege variable with the array $donnees["Privilege"] from the table.[color=#878a85]*/[/size][/color]
    $Privilege= $donnees["Privilege"];
    $Username= $donnees["Username"];
    }
     
    $req->closeCursor(); // Termine le traitement de la requête
     
    //we verify if the person is admin 
     
    if ($Privilege=="ADMIN")
    {
    // we create a session for the administrator 
    $_SESSION["Usertype"] = "ADMIN";
    $_SESSION["Username"] = $Username;
     
    // on rediriger le visiteur vers la page administrateur
    header('Location:adminpage.php');
    }
    elseif($Privilege=="USER") // if the person is a user
    {
    // we create a session for the User
    $_SESSION["Usertype"] = "USER";
    $_SESSION["Username"] = $Username ;
     
    // on rediriger le visiteur vers la page homepage
    header('Location:homepage.php');
    }
    }
    }
    ?>
     
    <!DOCTYPE html>
    <html >
    <head>
    <meta charset="UTF-8">
    <title>Esquina Estudiantil Form</title>
    <link rel="stylesheet" href="css-files/login-Style.css">
    </head>
    <body>
    <!--<div class="esquina-estudiantil">
    <div class="box_text">
    <h1><span class="text"> Esquina Estudiantil</span></h1>
    </div>
    </div>-->
    <div class="login-page">
    <div class="form">
    <form class="login-form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <label for="Username"></label>
    <input type="text" id="Username" name="Username" placeholder="Usuario" class="login-input"  autocomplete="on"/>
    <span class="error"><?php echo $nameErr;?></span>
    <label for="Password"></label>
    <input type="password" placeholder="Contraseña" id="Password" name="Password" class="login-input"  autofocus="ON" />
    <span class="error"><?php echo $passwordErr;?></span>
    <button type="submit" name="Privilege" value="USER" class="login-btn">ENTRAR</button>
    <p class="message">Que Esperas! <a href="register.php"><em>Hazte miembro</em></a></p>
    </form>
    </div>
    </div>
    <!--<script src='[url=http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js]http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js[/url]'></script>-->
    <script src="js/index.js"></script>
    </body>
    </html>

    post-199573-0-51630700-1469580573_thumb.png

  2. Post your code.

     

    The solution is to first do all PHP processing, and then generate the HTML markup:

    <?php
    
    // PHP code goes here
    
    ?>
    <!DOCTYPE HTML>
    
    <!-- HTML markup goes here -->
    

    This way there will never be any output before a header() call. It also leads to much cleaner and more maintainable code, because the business logic (PHP) is cleanly separated from the representational part (HTML).

    I tried it and it worked !!! thank you so much !!! 

  3. That's silly. When your code doesn't do what it should, the solution is to fix it, not make your users click on links.

     

    You'll probably encounter the same problem many more times, so why don't you take the chance to understand it and avoid it in the future?

    if you can give me any idea, i would be really grateful. the fact is that i made somee researches, and based on waht i found, it was like there nothing i could do to fix it. the only other solution i saw, was to send all the data to another page <action="verify.php"> and then retrieve the data with $_POST[] and make the connection to the database, after storing the data in tables, redirect user to homage with (header) but my problem was, what if the user didnt enter all the data from the form. i would like to send an error message to notify him there is a data missing but i am not good enough to do this, so i had to find a solution. but if you have any idea, please tell me.

  4. i found another solution. what i wanted to do is to redirect automaticly   my user to the homepage after registering his data input in the data base after registering on my webpage. so i created a variable $redirect = ''thanks for registering, please click here to get access to all our services" and then i put a link <a href="homapage.php">Homepage</a>. thus he can keep going by himself on the next pages.

  5. hello there! i am new to php ! i was trying to create a login form. in line 202

     

    <form class="login-form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

     

    so i can make the verification process on the same page and send the errors to the users when there are some

    .

    if there is no error (i.e all data correct) process data and make connection to database(mysql), put the data(username, password,

     

    email) in table, then at least redirect user to the home page with (header('Location: homepage.php'). but i got an error while loading

     

    page. I know why the problem. I just want someone to give me an advice on how to solve it. please help me !!!!

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.