Вопрос

Upon Logging in, I have the userID stored in the SESSION. However when I call updateMarkerlocations.php it says userID is undefined. Not sure what I'm missing.

login.php

session_start();
if (!isset($_POST['submit'])){

} else {
require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
    exit();
}
 $username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * from userinfo WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
    echo "<p>Invalid username/password combination</p>";
} else {
 $row = $result->fetch_assoc();

  setcookie("username", time() +60*60*24*30*365);
$_SESSION['userID'] = $row['userID']; 

     echo "<p>Logged in successfully!, Please close the window</p>";
}
}
?>       

updateMarkerLocations.php

 <?php
include 'db_const.php';


function insertMarkerLocations()
{
$markerCount = 0;
if (isset($_POST['markerCount']))
$markerCount = $_POST['markerCount'];

if(isset($_SESSION["userID"]))
{
 $userID = $_SESSION["userID"];
}

$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);

mysql_select_db(DB_NAME);

$userID = $_POST['userID'];

for($i=0 ; $i < $markerCount; $i++){ 
      $index = $i;
      ++$index;
      $curMarkerID = $_POST["markerID$index"];
      $curLang = $_POST["lang$index"];
      $curLat = $_POST["lat$index"];
  // Now write the current marker details in to the db.
  $query = "INSERT INTO userinfo (userID, markerID, lang, lat ) VALUES ('$userID', '$curMarkerID', '$curLang', '$curLat')";
  mysql_query($query)
    or die(mysql_error());
}
$msg = "SUCCESS";
return $msg;
}

 $msg = insertMarkerLocations();
echo json_encode($msg);
 ?>
Это было полезно?

Решение

Add this at the top of each file:

if(!isset($_SESSION)) session_start();

Also, when you do:

$userID = $_POST['userID'];

you should ensure that $_POST['userID'] exists:

if(isset($_POST['userID'])) $userID = $_POST['userID'];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top