简体   繁体   中英

500: Internal Server Error on if(iffset($_GET())) on ajax call

This is really blowing my mind. I'm doing a simple login function using jquery ajax to store the some user information in session variables. However, every time I run it I get a 500.

php file:

 <?php session_start(); if (isset($_GET("FirstName"))) { $_SESSION["ShopUserFirstName"] = htmlentities($_GET['FirstName']); $_SESSION["ShopUserLastName"] = htmlentities($_GET['LastName']); $_SESSION["ShopUserID"] = htmlentities($_GET['UserID']); echo "You are now logged in. first name:". $_SESSION["ShopUserFirstName"]." last name:".$_SESSION["ShopUserLastName"]."id:". $_SESSION["ShopUserID"]; //} else { // echo "shoplogin error"; } ?>

However, I discovered if I remove this check:

 if (isset($_GET("FirstName"))) { ... }

it works completely and everything is stored. Initially I was checking all 3 but either way it doesn't work. I've honestly never seen this behavior before, and I've used similar code many times in the past. Obviously I'd like to have that check there before I run any of the other code, can anyone help me unravel this one?

Here is my javascript as well:

 function login(first_name, last_name, user_id) { $.ajax({ method: "GET", url: "shoplogin.php", data: {'FirstName': first_name, 'LastName': last_name, 'UserID': user_id}, success: function (response) { alert(response); location.reload(); }, error: function (xhr, err) { alert("readyState: " + xhr.readyState + "\\nstatus: " + xhr.status); alert("responseText: " + xhr.responseText); } }); } $(".user").click(function () { var user_id = this.getAttribute("id"); var first_name = document.getElementById(user_id).getElementsByTagName('p')[0].innerHTML; var last_name = document.getElementById(user_id).getElementsByTagName('p')[1].innerHTML; login(first_name, last_name, user_id); });

Use:

if (isset($_GET["FirstName"])) {
//             ^           ^

An associative array of variables passed to the current script via the URL parameters.

$_GET is the associative array of parameters sent in the URL so, you can get the param value by using [] operator.

Documentation

$_POST and $_GET are arrays, so you should use ['index'] to access their values.

In this case use isset($_GET["FirstName"]) .

Hope it helps you.

An associative array of variables passed to the current script via the URL parameters.

Use [] instead of ()

<?php

session_start();

if (isset($_GET["FirstName"])) {

    $_SESSION["ShopUserFirstName"] = htmlentities($_GET['FirstName']);

    $_SESSION["ShopUserLastName"] = htmlentities($_GET['LastName']);

    $_SESSION["ShopUserID"] = htmlentities($_GET['UserID']);

    echo "You are now logged in. first name:". $_SESSION["ShopUserFirstName"]." last name:".$_SESSION["ShopUserLastName"]."id:". $_SESSION["ShopUserID"];

//} else {
//  echo "shoplogin error";
}
?>

GET Detalis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM