2017-04-10 1 views
0

Ich baue einen Studienplaner, der nach dem Einloggen nur einen einzigen Willkommensbereich enthält. Ich versuche derzeit, die Benutzer-ID des aktuell angemeldeten Benutzers zu erhalten Verwenden Sie in diesem Begrüßungsbereich eine SQL-Aktualisierungsabfrage sowie den Vornamen des Benutzers zur Verwendung in der Willkommensnachricht.Art Unterstützung beim Ziehen Momentan angemeldete Benutzer-ID und Vorname aus der Datenbank

ich versucht habe $_SESSION['user_id'] = userid; und $_SESSION['user_firstname'] = firstname; nach $_SESSION['login_user'] = $username;($_SESSION['login_user'] = $username; funktioniert gut durch die Art und Weise setzen), sondern auf in der Seite anmelden, erhalte ich diese Fehler: Notice: Use of undefined constant userid - assumed 'userid' in C:\wamp64\www\justread\session.php on line 11 und Notice: Use of undefined constant firstname - assumed 'firstname' in C:\wamp64\www\justread\session.php on line 12.

Nun, ich weiß, die Fehler wollen, dass ich zuerst eine Art Initialisierung für 'userid' und 'firstname' mache, bevor ich sie benutze, um eine Sitzungsvariable zu erstellen, aber ich bin nicht sicher, wie ich das machen soll frage mich, ob mir jemand helfen könnte.

Wir danken Ihnen im Voraus.

kann ich mehr Codes schreiben, falls erforderlich, aber die Codes ich besorgt glauben sind:

login.php:

<?php 

// Start session 
session_start(); 
// Variable to store error message 
$error =""; 
// If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted... 
if (isset($_POST['submit'])) { 
    // If username or password is not provided... 
    if (empty($_POST['username']) || empty($_POST['password'])) { 
     // ...tell user that login details are invalid. 
     $error = "Please fill in both your username and your password"; 
     // Else... 
    } else { 
     // ...put the provided username and password in variables $username and $password, respectively 
     $username = $_POST['username']; 
     $password = $_POST['password']; 
     // Establish connection to the server 
     $mysqli = mysqli_connect("localhost", "root", ""); 
     // set up measures to counter potential MySQL injections 
     $username = stripslashes($username); 
     $password = stripslashes($password); 
     $username = mysqli_real_escape_string($mysqli, $username); 
     $password = mysqli_real_escape_string($mysqli, $password); 
     // Select Database 
     $db = mysqli_select_db($mysqli, "p00702"); 
     // SQL query to fetch information of registerd users and find user match. 
     $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'"); 
     // Return the number of rows of the query result and put it in $rows variable 
     $rows = mysqli_num_rows($query); 
     // If rows are equal to one... 
     if ($rows == 1) { 
      unset($_SESSION['error']); 
      // Initialize session with the username of the user... 
      $_SESSION['login_user'] = $username; 
      // Set the user ID of the user 
      $_SESSION['user_id'] = userid; 
      // Set the user first name of the user 
      $_SESSION['user_firstname'] = firstname; 
      // ...and redirect to the homepage. 
      header("Location: welcome.php"); 
      // Make sure that codes below do not execut upon redirection. 
      exit; 
     // Else, 
     } else { 
      // and tell user that the login credentials are invalid. 
      $error = "Your username or password is invalid"; 
      $_SESSION['error'] = $error; 
      // redirect user to the home page (index.php) 
      header("Location: index.php"); 
     } 
     // ...and close connection 
     mysqli_close($mysqli); 
    } 
} 

session.php

<?php 

// Establish connection to the server 
$mysqli = mysqli_connect("localhost", "root", ""); 
// Selecting Database 
$db = mysqli_select_db($mysqli, "p00702"); 
// Starting session 
session_start(); 
// Storing Session 
$user_check = $_SESSION['login_user']; 
$_SESSION['user_id'] = userid; 
$_SESSION['user_firstname'] = firstname; 
// Test to see the content of the global session variable 
print_r($_SESSION); 
// SQL Query To Fetch Complete Information Of User 
$ses_sql = mysqli_query($mysqli, "SELECT username FROM logins WHERE username='$user_check'"); 
$row = mysqli_fetch_assoc($ses_sql); 
$login_session = $row['username']; 
if (!isset($login_session)) { 
    // Closing Connection 
    mysqli_close($mysqli); 
    // Redirecting To Home Page 
    header('Location: index.php'); 
    // Make sure that codes below do not execut upon redirection. 
    exit; 
} 

Antwort

0

In PHP, Variablennamen beginnt mit '$' Zeichen. Außerdem müssen Sie in login.php die Daten mit mysqli_fetch_row oder einer ähnlichen Funktion abrufen. Ich gehe davon aus, dass Sie nach dem Login in die session.php weiterleiten. In diesem Fall müssen Sie den Session-Variablen nichts zuweisen. Es wird schon da sein. Alles, was Sie tun müssen, ist es zuzugreifen.

login.php

<?php 

// Start session 
session_start(); 
// Variable to store error message 
$error =""; 
// If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted... 
if (isset($_POST['submit'])) { 
    // If username or password is not provided... 
    if (empty($_POST['username']) || empty($_POST['password'])) { 
     // ...tell user that login details are invalid. 
     $error = "Please fill in both your username and your password"; 
     // Else... 
    } else { 
     // ...put the provided username and password in variables $username and $password, respectively 
     $username = $_POST['username']; 
     $password = $_POST['password']; 
     // Establish connection to the server 
     $mysqli = mysqli_connect("localhost", "root", ""); 
     // set up measures to counter potential MySQL injections 
     $username = stripslashes($username); 
     $password = stripslashes($password); 
     $username = mysqli_real_escape_string($mysqli, $username); 
     $password = mysqli_real_escape_string($mysqli, $password); 
     // Select Database 
     $db = mysqli_select_db($mysqli, "p00702"); 
     // SQL query to fetch information of registerd users and find user match. 
     $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'"); 
     // Return the number of rows of the query result and put it in $rows variable 
     $rows = mysqli_num_rows($query); 
     // If rows are equal to one... 
     if ($rows == 1) { 
      $row = mysql_fetch_object($query); 
      unset($_SESSION['error']); 
      // Initialize session with the username of the user... 
      $_SESSION['login_user'] = $username; 
      // Set the user ID of the user 
      $_SESSION['user_id'] = $row->userid; 
      // Set the user first name of the user 
      $_SESSION['user_firstname'] = $row->firstname; 
      // ...and redirect to the homepage. 
      header("Location: welcome.php"); 
      // Make sure that codes below do not execut upon redirection. 
      exit; 
     // Else, 
     } else { 
      // and tell user that the login credentials are invalid. 
      $error = "Your username or password is invalid"; 
      $_SESSION['error'] = $error; 
      // redirect user to the home page (index.php) 
      header("Location: index.php"); 
     } 
     // ...and close connection 
     mysqli_close($mysqli); 
    } 
} 

session.php

<?php 

// Establish connection to the server 
$mysqli = mysqli_connect("localhost", "root", ""); 
// Selecting Database 
$db = mysqli_select_db($mysqli, "p00702"); 
// Starting session 
session_start(); 

if (!isset($_SESSION['user_id'])) { 
    // Closing Connection 
    // Redirecting To Home Page 
    header('Location: index.php'); 
    // Make sure that codes below do not execut upon redirection. 
    exit; 
} 
print_r($_SESSION); 

Auch das Verbindungsteil zu einer separaten Datei verschieben und sie in allen Skripten, so dass, wenn Sie Ihre Anmeldeinformationen zu ändern, Sie don Ich muss es in allen Dateien ändern.

+0

Vielen Dank für die Rückmeldung an mich. Ich habe folgende Zeilen eingefügt: $ row = mysql_fetch_object ($ query); $ _SESSION ['user_id'] = $ row-> Benutzer-ID; $ _SESSION ['user_firstname'] = $ row-> Vorname; aber es kommt immer noch mit den gleichen Fehlern. Gibt es noch etwas, das ich vermisse? –

+0

Überprüfen Sie Ihre Session.php-Datei. Das sollte diese Zuordnungsoperation überhaupt nicht haben. –

+0

Hey, ich komme irgendwo hin. Die Benutzer-ID wird angezeigt, ich muss nur den Vornamen-Aspekt sortieren. Ich werde Ihnen Bescheid geben. Danke nochmal. –

Verwandte Themen