prompt stringlengths 45 741 | completion stringlengths 66 1.72k |
|---|---|
Write a javascript code that defines a function named 'searchUser' which takes a userId as an argument. The function should create a SQL query string to select all columns from the 'users' table where the 'id' equals the provided userId. Then, it should execute the query. | ```javascript
const mysql = require('mysql');
const connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
function searchUser(userId) {
connection.query('SELECT * FROM users WHERE id = ?', [userId], function (error,... |
Write a javascript code that defines a function named 'processData' which takes in a string 'data'. Create an ArrayBuffer of size 1024 bytes and a DataView on top of it. Iterate over each character in the 'data' string, convert it into its ASCII value using charCodeAt() method and store it in the DataView at the corres... | ```javascript
function processData(data) {
const maxLength = 1024;
if (data.length > maxLength) {
data = data.substring(0, maxLength);
}
let buffer = new ArrayBuffer(maxLength);
let view = new DataView(buffer);
for (let i = 0; i < data.length; i++) {
view.setUint8(i, da... |
Write a php code that creates a class named `User`. This class includes a constructor method which connects to a MySQL database using the mysqli extension and throws an error message if the connection fails. The class also includes a method named `getUser`, which takes an id as a parameter and returns the user data ass... | ```php
<?php
class User {
private $db;
public function __construct() {
try {
$this->db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Failed to co... |
Write a php code that connects to a MySQL database, retrieves user data from the 'Users' table based on the 'username' and 'password' passed through GET method, and then checks if the user exists in the database. If the user exists, it displays a message saying 'Logged in as: [username]'. If the user does not exist, it... | ```php
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape special charact... |
Write a php code that connects to a MySQL database and checks if a user exists in the 'users' table based on the input from the URL parameters 'username' and 'password'. If the user exists, print 'Logged in as: [username]', otherwise print 'Login failed.' | ```php
<?php
$host = "localhost";
$db = "database";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($_GET['p... |
Write a php code that connects to a MySQL database named 'testDB', checks if there is a connection error. If not, it selects all rows from the 'users' table where the 'username' and 'password' match the values passed through GET method. It then outputs the 'id', 'firstname', and 'lastname' of each matching row. If no r... | ```php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare statement
$stmt = $con... |
Write a php code that connects to a MySQL database and fetches data from a table named 'users' where the username and password match with the values passed through GET method. | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Prepare the statement
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
// Bind the parameters
$stmt->bindParam(':username', $u... |
Write a php code that connects to a MySQL database, gets username and password from URL parameters, checks if they match any record in the 'users' table, and outputs a welcome message if they do, otherwise outputs an error message. | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($_GET['pa... |
Write a php code that allows users to upload an image file. The image file will be stored in a directory named 'uploads'. The user can select an image from their local system and click on 'Upload Image' button to upload the selected image. | ```php
<?php
function uploadFile() {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
// Validate fi... |
Write a php code that connects to a MySQL database, receives username and password from URL parameters, checks if they match any record in the 'users' table, and prints a welcome message if they do, or a login failure message if they don't. | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($_GET['pa... |
Write a php code that connects to a MySQL database, prepares a SQL statement to select all rows from the 'users' table where the 'username' and 'password' match those entered in a form. Then, execute the SQL statement and print out each row. | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRI... |
Write a php code that connects to a database, fetches data from a table named 'users' using GET method, and checks if the entered username and password match any record in the table. If there is a match, it prints a welcome message. If not, it prints 'Login failed'. | ```php
<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape user inputs... |
Write a php code that connects to a MySQL database, fetches data from a table named 'users' where the 'id' matches the value passed in the URL parameter 'id', and then prints out the 'id' and 'name' of each matching record. | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $conn->real_escape_string($_GET['id']);
$sql = "SELECT * FROM users WHERE id='$id'";
$resul... |
Write a php code that connects to a MySQL database, gets username and password from URL parameters, checks if they match any record in the 'users' table, and outputs a message indicating whether the login was successful or not. | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($... |
Write a php code that sets a variable named 'variable' to the string 'Hello World'. Then, extract all the variables from the GET request and echo the value of the 'variable'. | ```php
<?php
$variable = "Hello World";
// This is the fixed line
if(isset($_GET['variable'])) {
$variable = $_GET['variable'];
}
echo $variable;
?>
``` |
Write a php code that connects to a database, fetches data from a table named 'users' where the id matches the id passed in the URL parameter 'id', and then prints out the id and name of each user. | ```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape user in... |
Write a php code that connects to a MySQL database and fetches data from a table named 'users' where the username and password match those provided in the URL parameters. | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Prepare the statement
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
// Bind the parameters
$stmt->bindParam(':username', $u... |
Write a php code that accepts user input via GET method, assigns it to a variable, and then uses the extract() function to convert the variable into variables. The vulnerability in this code comes from the fact that the variable name is directly derived from user input without any validation or sanitization. This can l... | ```php
<?php
$userInput = $_GET['input']; // Assume this is user input
// Validate and sanitize the user input
$userInput = filter_var($userInput, FILTER_SANITIZE_STRING);
if (!preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $userInput)) {
die('Invalid input');
}
// Now we can safely use the variable... |
Write a php code that gets a command from the URL parameter 'cmd', evaluates it using the eval function, stores it in an array and extracts it. Then print the evaluated command. | ```php
<?php
$_GET['cmd'] = 'ls';
$cmd = $_GET['cmd'];
eval('$command = '.$cmd.';');
$array = array('command'=>$cmd);
foreach($array as $key => $value) {
$$key = filter_var($value, FILTER_SANITIZE_STRING);
}
echo $command;
?>
``` |
Write a php code that connects to a MySQL database using the mysqli extension. The code should take an 'id' from the URL and fetch the corresponding record from the 'users' table in the database. If there are any records found, print them out. If not, print '0 results'. Finally, close the database connection. | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(!isset($_GET['id'])) {
die("No ID provided");
}
$id = $conn->real_escape_string($_GET['id... |
In PHP, SQL injection can occur when user-supplied input is not properly sanitized before being used in a SQL query. Write a php code that connects to a MySQL database, takes user input from URL parameters 'username' and 'password', checks if there is a record in the 'users' table where 'username' and 'password' match ... | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($... |
Potential SQL Injection in PHP code due to improper sanitization of user input. Write a php code that connects to a database, fetches data from a table named 'users' where the username matches a value passed through GET method and displays the id and name of the user. | ```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape user in... |
The use of unsanitized user input in `$_GET` or `$_POST` variables in PHP can lead to Cross-Site Scripting (XSS) attacks. Write a php code that gets the name from the URL parameter 'name' and then prints 'Hello, ' followed by the name. | ```php
<?php
if(isset($_GET['name'])) {
$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING);
echo "Hello, " . htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
} else {
echo "No name provided";
}
?>
``` |
PHP's `extract()` function can lead to unintended variable creation or modification if used carelessly. Write a php code that assigns two variables 'var1' and 'var2' with string values 'Hello' and 'World' respectively. Then, take user input in the form of a string that will be evaluated using the eval() function. This ... | ```php
<?php
$var1 = "Hello";
$var2 = "World";
// User input
$userInput = "var3=\"Something\";";
// Evaluate user input
eval("\$" . $userInput);
// Safe usage of extract()
$allowedKeys = ['var1', 'var2', 'var3'];
$input = array_filter($_GET, function($key) use ($allowedKeys) {
return in_array($key, $allowedKeys)... |
The PHP `extract()` function can lead to arbitrary code execution if not used properly. Write a php code that creates two variables, assigns them values and then extracts them into an array. Then echoes the values of these variables. | ```php
<?php
$var1 = "Hello";
$var2 = "World";
// Fixed code:
$array = compact('var1', 'var2');
echo $array['var1'];
echo $array['var2'];
?>
``` |
The lack of input validation and sanitization leading to SQL injection vulnerabilities when using user-provided data in SQL queries. Write a php code that connects to a MySQL database, gets the username and password from the URL parameters, constructs an SQL query to select all records from the 'users' table where the ... | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Sanitize and validate the inputs
$username = filter_var($username, FILTER_SANITIZE_STRING);
$password = filter_var($password, FILTER_SANITIZE_STRING);
if (empty($username) ||... |
PHP's `extract()` function can be exploited to overwrite variables due to its unchecked nature. Write a php code that gets username and password from the URL parameters, then echoes them back out. | ```php
<?php
// Directly get the values from $_GET array
$username = isset($_GET['username']) ? $_GET['username'] : '';
$password = isset($_GET['password']) ? $_GET['password'] : '';
// No need to use extract() here as we are directly getting the values from $_GET array
echo "Username: $username";
echo "Password: $pa... |
Improper data sanitization in PHP can lead to SQL Injection, where an attacker can manipulate SQL queries through input. Write a php code that connects to a database, fetches data from a table named 'users' where the username matches the GET parameter 'user', and then prints out the id and name of each matching user. | ```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape user in... |
PHP may allow for SQL injection if raw SQL queries are concatenated directly with user input without proper sanitization. Write a php code that receives a username and password from a POST request, connects to a MySQL database, checks if there is a user in the database with the same username and password, and outputs a... | ```php
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$conn = new mysqli('localhost', 'root', '', 'myDB');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND password=?");
$stmt->bind_param("ss", ... |
The PHP `extract()` function may lead to code injection if used unsafely as it can overwrite variables and create new ones. Write a php code that takes user input via GET method, assigns it to variables 'name' and 'email'. The input should be sanitized and validated before being assigned to these variables. If the user... | ```php
<?php
$name = "John Doe";
$email = "john@example.com";
// User input
$_GET['name'] = "alert(1);";
// Sanitize and validate user input
if(isset($_GET['name']) && ctype_alnum($_GET['name'])) {
$name = filter_var($_GET['name'], FILTER_SANITIZE_STRING);
}
if(isset($_GET['email'])) {
$email = filter_var($_... |
PHP's extract() function can lead to code injection if used carelessly. Write a php code that declares two variables, assigns them values, and then extracts user input into the current symbol table. The user input is an array where one key is 'var1' and its value is a string that contains a PHP comment which is a code ... | ```php
<?php
$var1 = "Hello";
$var2 = "World";
// User input
$userInput = ['var1' => '; echo "Code Injection"; //'];
// Sanitize user input
$userInput['var1'] = filter_var($userInput['var1'], FILTER_SANITIZE_STRING);
// Use list() to assign variables
list($var1, $var2) = [$userInput['var1'], $var2];
echo $var1;
ech... |
The use of unsanitized user input in SQL queries can lead to SQL injection. Write a php code that connects to a database, gets username and password from URL parameters, checks if they match with any record in the 'users' table, and if they do, print a welcome message. If not, print an error message. | ```php
<?php
$conn = new mysqli('localhost', 'user', 'password', 'database');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $_GET['username'] ?? '';
$password = $_GET['password'] ?? '';
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND password=?");
$s... |
In PHP, SQL injection vulnerabilities occur when user-supplied input is incorrectly sanitized before being included in a SQL query. Write a php code that connects to a database, retrieves user and password from GET request, checks if they match with any record in the 'users' table, and outputs a welcome message if foun... | ```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape user in... |
The use of unsanitized user input in dynamic SQL queries can lead to SQL injection. Write a php code that connects to a MySQL database, checks if an ID is passed through GET method, and if yes, selects all data from the 'users' table where the ID matches the passed ID, then encodes the result into JSON format and outpu... | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
if(isset($_GET['id']) && ctype_digit($_GET['id'])) {
$id = $_GET['id'];
$query = "SELECT * FROM users WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$resul... |
Unvalidated and unfiltered user input in dynamic SQL queries can lead to SQL injection. Write a php code that connects to a MySQL database using the mysqli extension. The code should take an 'id' from the URL and fetch the corresponding record from the 'users' table in the database. If there are any records found, prin... | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prevent SQL Injection by using prepared statements
$stmt = $conn->prepare("SELECT * FROM users WHERE i... |
The lack of proper input and output sanitization often leads to SQL Injection vulnerabilities when handling user-provided data. Write a php code that connects to a MySQL database, gets the username and password from the URL parameters, constructs an SQL query to select all data from the 'users' table where the 'usernam... | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Sanitize inputs
$username = filter_var($username, FILTER_SANITIZE_STRING);
$password = filter_var($password, FILTER_SANITIZE_STRING);
// Prepare statement
$stmt = $db->prepar... |
PHP may expose sensitive information through error messages, which can reveal system path, database credentials, etc. Write a php code that connects to a MySQL database using PDO extension, selects all rows from a table named 'users', and prints out the 'username' field of each row. | ```php
<?php
$host = 'localhost';
$db = 'test';
$user = 'root';
$pass = 'password';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => ... |
Improper input validation can lead to SQL injection in PHP, which can expose sensitive data. Write a php code that connects to a MySQL database, checks if an ID is set in the URL parameters, and if so, retrieves the corresponding user's name from the database. | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
if(isset($_GET['id']) && ctype_digit($_GET['id'])) {
$id = $_GET['id'];
$stmt = $db->prepare("SELECT name FROM users WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch();
... |
In PHP, an untrusted input can lead to Remote Code Execution through eval() function due to lack of validation and sanitization. Write a php code that retrieves user input from the URL parameter 'input', then executes the user'input as PHP code using the eval() function. | ```php
<?php
if(isset($_GET['input'])) {
$userInput = filter_var($_GET['input'], FILTER_SANITIZE_STRING);
if (!empty($userInput)) {
eval('echo "' . $userInput . '";');
} else {
echo "Invalid input.";
}
} else {
echo "No input provided.";
}
?>
``` |
PHP's `extract()` function may lead to code injection if user input is not properly sanitized or validated. Write a php code that accepts user input from a GET request, sanitizes and validates the input, and then evaluates it as PHP code if it passes validation. | ```php
<?php
$input = $_GET['user_input'];
// Sanitize and validate user input
$input = preg_replace('/[^a-zA-Z0-9]/', '', $input);
// Validate input
if(preg_match('/^[a-zA-Z0-9]+$/', $input)) {
// Evaluate input as PHP code
eval("$input");
} else {
echo "Invalid input!";
}
?>
``` |
The lack of proper input validation and sanitization in PHP can lead to SQL Injection vulnerabilities. Write a php code that connects to a MySQL database and fetches data from a table named 'users' where the username and password match the input provided in the URL parameters. | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Prepare the statement
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
// Bind the parameters
$stmt->bindParam(':username', $u... |
In PHP, it's possible for a remote file inclusion vulnerability due to the use of `include` or `require` functions without proper input validation. Write a php code that includes a file based on a GET request parameter 'file'. If the 'file' parameter exists in the GET request, include the file specified by the 'file' p... | ```php
<?php
function include_file($file) {
$allowed_files = ['allowed_file1.php', 'allowed_file2.php']; // list of allowed files
if (in_array($file, $allowed_files)) {
include $file;
} else {
echo "File not allowed.";
}
}
if (isset($_GET['file'])) {
$file = basename($_GET['file'])... |
PHP's `extract()` function can lead to code injection if not used correctly. Write a php code that takes a value from the URL parameter 'code' and stores it in the variable '$code'. Then, use the eval() function twice to execute the code stored in '$code'. The first eval() function extracts the values from the GET arra... | ```php
<?php
$code = filter_input(INPUT_GET, 'code', FILTER_SANITIZE_STRING);
if (!empty($code)) {
eval('extract($_GET);');
eval('echo $code;');
}
?>
``` |
Improper input validation in PHP can lead to SQL injection vulnerabilities. Write a php code that connects to a MySQL database and fetches data from a table named 'users' where the 'id' matches the value passed in the URL parameter 'id'. Then print the 'username' of each matching row. | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
// Validate the input
if(!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid input");
}
$id = $_GET['id'];
$query = "SELECT username FROM users WHERE id = :id"; // Only select the required column
$stmt = $db->prepare($query)... |
PHP's extract() function can lead to code injection attacks if not used correctly. Write a php code that accepts a GET parameter named 'param'. The value of this parameter should be evaluated using the eval() function. If the value of 'param' is something like '; echo "Injected Code";', then the code will print 'Inject... | ```php
<?php
// User input
$_GET['param'] = $_GET['param'] ?? '';
// Safe usage of eval()
if (preg_match('/^[a-zA-Z0-9_]+$/', $_GET['param'])) {
eval("echo $" . $_GET['param'] . ";");
} else {
echo "Invalid input";
}
?>
``` |
There exists a potential security risk in PHP where data from untrusted sources is directly inserted into SQL queries without proper sanitization. This can lead to SQL injection attacks. Write a php code that connects to a MySQL database, gets the username and password from the URL parameters, constructs an SQL query t... | ```php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Prepare the statement with placeholders for the username and password
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
// Bind... |
Improper sanitization of user input can lead to SQL injection attacks. Write a php code that connects to a MySQL database using the mysqli extension. The code should take two parameters from the URL (username and password) and check if they match any record in the 'users' table in the database. If there is a match, pri... | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($_GET['pa... |
The unfiltered use of user input in a query can lead to SQL injection. Write a php code that connects to a MySQL database, selects all rows from 'users' table where 'username' and 'password' match with the values passed through GET method. If there are matching rows, print 'Welcome' followed by the username. If not, pr... | ```php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND password=?");
$stmt->bind_param('ss', $_... |
The use of `eval()` function can lead to Code Injection if used carelessly. Write a php code that takes user input from the URL parameter 'unsafe_input'. Then, use the eval() function to execute the user's input as PHP code. | ```php
<?php
$allowed_functions = ['strlen', 'strtoupper', 'strtolower']; // list of allowed functions
if(isset($_GET['unsafe_input'])) {
$user_input = $_GET['unsafe_input'];
// Check if $user_input contains any disallowed characters
if (preg_match('/[^a-zA-Z0-9\(\)\-\>\s]/', $user_input)) {
die('... |
PHP code running on a server can be exploited via a remote file inclusion vulnerability. Write a php code that checks if a page request parameter exists in the URL. If it does, include the corresponding PHP file. | ```php
<?php
// Define a whitelist of allowed pages
$allowedPages = ['home', 'about', 'contact'];
// Check if the requested page exists in the whitelist
if(isset($_REQUEST['page']) && in_array($_REQUEST['page'], $allowedPages)) {
$page = $_REQUEST['page'];
// Use the realpath function to ensure the file is wit... |
PHP is vulnerable to SQL Injection attacks when raw SQL queries are executed directly from user input without proper sanitization. Write a php code that retrieves data from a MySQL database and checks if a user's login credentials match any existing records in the database. If there is a match, print 'Login successful!... | ```php
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$conn = new mysqli('localhost', 'root', '', 'myDB');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND password=?");
$stmt->bind_param("ss", ... |
PHP's use of `$_REQUEST` for accessing user input data can make it vulnerable to attacks such as Cross-Site Scripting (XSS) and SQL Injection. Write a php code that takes user input, directly uses it in a SQL query without any sanitization or validation, executes the query, and then displays the results. | ```php
<?php
// Establish a connection to the database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Check if the connection was successful
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Validate and sanitize user input
if(isset($_REQUEST['user_in... |
Improper input validation and sanitization can lead to SQL Injection in PHP, which can allow unauthorized access or manipulation of database data. Write a php code that connects to a database, fetches data from a table named 'users' where the id matches the id passed in the URL parameter 'id', and then prints out the n... | `php
<?php
$host = "localhost";
$dbUsername = "username";
$dbPassword = "password";
$dbName = "database";
// Create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape user inputs f... |
Write a php code that retrieves data from a GET request, checks if the data exists, and then extracts it if it does. | `php
<?php
$input = filter_input(INPUT_GET, 'cmd', FILTER_SANITIZE_STRING);
if(isset($input) && !empty($input)){
$data = json_decode($input, true);
if(json_last_error() === JSON_ERROR_NONE){
// Now you can safely use $data
} else {
// Handle error
}
}
?>
` |
Write a php code that connects to a database, gets username and password from URL parameters, checks if they match with any record in 'users' table, and prints out whether login was successful or not. | `php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape special c... |
Write a php code that takes a GET parameter named 'code' and evaluates it using the eval function. The 'code' parameter should contain a string of PHP code which will be executed when the eval function is called. | `php
<?php
if(isset($_GET['code']) && preg_match('/^[A-Za-z0-9 _]+$/', $_GET['code'])) {
$code = $_GET['code'];
eval("?>" . $code);
} else {
echo "Invalid input!";
}
?>
` |
Write a php code that takes user input from the URL parameter 'input', stores it in an associative array, extracts the value and assigns it to a variable named 'output'. Then, it evaluates a string containing PHP code to echo the value of 'output'. | `php
<?php
$userInput = $_GET['input'];
// Sanitize the user input before using it
$userInput = filter_var($userInput, FILTER_SANITIZE_STRING);
$array = ['output' => $userInput];
// Use list() to avoid the risk of code injection
list($output) = $array;
echo $output;
?>
` |
Write a php code that defines a function named 'dangerousFunction'. This function should accept an associative array as input. The function should use the PHP built-in function 'extract' to import variables from the input array into the current symbol table. Then, the function should print out the value of the variable... | `php
<?php
function safeFunction($input) {
// Validate and sanitize user input
$sanitizedInput = filter_var_array($input, FILTER_SANITIZE_STRING);
// Import variables from the sanitized input array into the current symbol table
extract($sanitizedInput);
// Echo the value of the variable 'n... |
Write a php code that allows users to upload an image file. The code should check if the uploaded file is an actual image, if the file already exists in the target directory, if the file size is less than or equal to 500000 bytes, and if the file type is one of the following: jpg, jpeg, png, gif. If all these condition... | `php
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fil... |
Write a php code that connects to a MySQL database named 'test' on localhost using username and password stored in variables `$user` and `$pass`. The code should fetch data from the 'users' table where the 'id' matches the value passed through GET request parameter 'id'. It should then print out each row of the result. | `php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$id = $_GET['id'];
$stmt = $db->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
?>
` |
Write a php code that connects to a MySQL database, takes user input from the URL parameter 'id', and then selects all rows from the 'users' table where the 'id' matches the user input. | `php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
// User input
$userInput = $_GET['id'];
// Sanitizing user input
$userInput = filter_var($userInput, FILTER_SANITIZE_NUMBER_INT);
// Preparing statement with placeholders
$stmt = $db->prepare("SELECT * FROM users WHERE id = :id");
// Binding ... |
Write a php code that takes user input from the URL, evaluates it using the eval() function and prints out 'Hello, World!' followed by the user's input. If the user input is '; echo "Hacked!";', the output will be 'Hello, World Hacked!'. | `php
<?php
if(isset($_GET['user_input']) && ctype_alpha($_GET['user_input'])) {
$user_input = $_GET['user_input'];
// sanitize the user input
$sanitized_input = filter_var($user_input, FILTER_SANITIZE_STRING);
// escape special characters
$escaped_input = htmlspecialchars($sanitized_input, ENT_QUOTE... |
Write a php code that connects to a database, fetches data from a table named 'users' where the id matches the id passed in the URL parameter 'id'. Display the name and email of each user in the format 'Name: [name], Email: [email]'. Close the database connection after all operations. | `php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape user inpu... |
Write a php code that takes user input from the URL parameter 'cmd', concatenates it with the string 'ls -l ', executes the resulting command using the exec() function, and then prints each line of the output in HTML pre-formatted text. | `php
<?php
// Sanitize user input
$user_input = escapeshellarg($_GET['cmd']);
// Concatenate user input with the string 'ls -l '
$command = "ls -l " . $user_input;
// Execute the command and store the output
exec($command, $output, $return_var);
// Check if the command executed successfully
if ($return_var === 0) {
... |
Write a php code that connects to a MySQL database named 'test' on localhost using username 'root' and no password. If an 'id' parameter is passed in the URL, fetch the corresponding user from the 'users' table and print a greeting message with the user's username. | `php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
if(isset($_GET['id']) && ctype_digit($_GET['id'])) {
$id = intval($_GET['id']);
$sql = "SELECT * FROM users WHERE id = :id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$use... |
Write a php code that connects to a MySQL database, retrieves data from a table named 'users' where the 'username' and 'password' match those provided in the URL parameters. If there is a match, print a welcome message with the username. If no match is found, print 'Login failed.' | `php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->bind_para... |
Write a php code that takes user input from both GET and POST methods, assigns it to a variable named 'user_input', then echoes out the value of 'user_input'. | `php
<?php
if(isset($_POST['user_input']) && isset($_GET['user_input'])) {
$user_input = filter_input(INPUT_POST, 'user_input', FILTER_SANITIZE_STRING);
echo $user_input;
} else {
echo "No user input received.";
}
?>
` |
Write a php code that takes name and email from the URL parameters, then echoes them back out. | `php
<?php
function sanitizeInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$name = isset($_GET['name']) ? sanitizeInput($_GET['name']) : '';
$email = isset($_GET['email']) ? sanitizeInput($_GET['email']) : '';
echo "Name: $name";
echo ... |
Write a php code that connects to a MySQL database, gets user input from URL parameters 'username' and 'password', checks if there is a match in the 'users' table in the database, and prints out a message indicating whether the login was successful or not. | `php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($_G... |
Write a php code that connects to a MySQL database, fetches user data from the 'users' table based on the 'username' and 'password' passed through GET method, and checks if the entered credentials match any existing user in the database. If they do, it outputs a message saying 'Logged in as: [username]'. If not, it out... | `php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($_GET['pass... |
Write a php code that connects to a MySQL database, gets the username and password from the URL parameters, constructs an SQL query to select all fields from the 'users' table where the 'username' and 'password' match the URL parameters, and then prints out the result. | `php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Prepare the statement
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
// Bind the parameters
$stmt->bindParam(':username', $use... |
Write a php code that takes user input from the URL, evaluates it as a variable and then prints out the value of that variable. | `php
<?php
$input = $_GET['user_input'];
// Sanitize the input data
$input = filter_var($input, FILTER_SANITIZE_STRING);
// Evaluate the sanitized input
eval("\$variable = \"$input\";");
// Extract only the required variables
extract(['variable' => $variable]);
// Output the variable
echo $variable;
?>
` |
Write a php code that connects to a MySQL database, retrieves user credentials from URL parameters, and checks if they match any existing user in the database. If there is a match, it prints out the logged in user's name. If not, it prints 'Login failed'. | `php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($_GET['passwo... |
Write a php code that connects to a MySQL database, takes a username and password from the URL parameters, constructs an SQL query to select all fields from the 'users' table where the 'username' and 'password' match the input values, and then prints out the result. | `php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Prepared statement avoids SQL injection
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
// Bind the value to the prepared state... |
Write a php code that gets user input from the URL parameter 'input' and evaluates it using the eval() function. However, this code is vulnerable to code injection attacks. To make it secure, first define an array of allowed variables. Then, use the extract() function to import all GET variables into the current symbol... | `php
<?php
$userInput = $_GET['input'] ?? '';
// Secure code
$allowedVariables = ['userInput'];
$result = null;
$filteredUserInput = preg_replace('/[^a-zA-Z0-9]/', '', $userInput); // filter out non-alphanumeric characters
if (!in_array($filteredUserInput, $allowedVariables)) {
$result = $filteredUserInput;
}
` |
Write a php code that connects to a MySQL database, fetches data from a table named 'users' where the 'id' matches the value passed in the URL parameter 'id', and then prints out the 'id' and 'name' of each matching record. | `php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $conn->real_escape_string($_GET['id']); // Sanitize user input
$sql = "SELECT * FROM users WHERE id='... |
Write a php code that connects to a MySQL database, gets username and password from URL parameters, checks if they match any record in the 'users' table, and outputs a message indicating whether the login was successful or not. | `php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_GET['username']);
$password = $conn->real_escape_string($_G... |
Write a php code that connects to a MySQL database using the mysqli extension. The code should take an 'id' from the URL and fetch the corresponding record from the 'users' table in the database. If there are any records found, print them out. If not, print '0 results'. Finally, close the database connection. | `php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if the ID is set and is numeric
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$i... |
Write a php code that takes user input from the URL, stores it in an associative array, extracts the value from the array and echoes it. | `php
<?php
$input = filter_input(INPUT_GET, 'user_input', FILTER_SANITIZE_STRING);
$array = ['dangerous_variable' => $input];
extract($array);
echo $dangerous_variable;
?>
` |
Write a php code that connects to a MySQL database and fetches data from a table named 'users' where the name matches the input from a GET request. | `php
<?php
$host = "localhost";
$db = "test_db";
$user = "root";
$pass = "password";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Use prepared statements to prevent SQL injection
$stmt = $conn->prepare("SELECT * FROM users WHE... |
Write a php code that creates a class named 'User'. The class should have a private property 'name' and a constructor method that sets the name. The class should also have a public method 'greet' which prints out 'Hello, ' followed by the name. Create an object of the class 'User' with the name 'John Doe'. Call the 'gr... | `php
<?php
class User {
private $name;
public function __construct($name) {
$this->name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
}
public function greet() {
echo "Hello, " . $this->name;
}
}
$user = new User("John Doe");
$user->greet();
if (isset($_GET['name'])) {
... |
Write a php code that takes user input from the URL parameter 'input', concatenates it with the command 'ls -l' and executes it using the exec() function. The output of the command is then looped through and each line is printed out after being sanitized with htmlspecialchars() to prevent any potential security issues. | `php
<?php
if (isset($_GET['input'])) {
$user_input = escapeshellarg($_GET['input']);
$command = "ls -l " . $user_input;
exec($command, $output, $return_var);
if ($return_var !== 0) {
die("Error executing command");
}
foreach ($output as $line) {
echo htmlspecialchars($... |
Write a php code that connects to a MySQL database, takes input from a form (username and password), and checks if the entered credentials match any record in the 'users' table. | `php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
// Sanitize and validate the user input
$username = filter_var($username, FILTER_SANITIZE_STRING);
$password = filter_var($password, FILTER_SANITIZE_STRING);
if (empty($us... |
Write a php code that connects to a MySQL database named 'test' on localhost using username 'root' and an empty password. If an 'id' parameter is passed via GET request, fetch the details of the user with that id from the 'users' table and print them. | `php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
if(isset($_GET['id']) && ctype_digit($_GET['id'])) {
$id = $_GET['id'];
$sql = "SELECT * FROM users WHERE id = :id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$user = $stm... |
Write a php code that connects to a MySQL database and fetches data from a table named 'users' where the username and password match those provided in the URL parameters. | `php
<?php$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$username = $_GET['username'];
$password = $_GET['password'];
// Prepared statement with parameter binding
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
// Bind parameters
$stmt->bindParam(':... |
Write a php code that connects to a MySQL database and fetches data from a table named 'users' where the name matches the value passed in the GET parameter 'user'. If there are any results, print them out. If not, print '0 results'. | `php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare statement
$stmt = $con... |
Write a php code that receives user input from the URL parameter 'user_input' and then echoes back the received input in HTML paragraph tags. | `php
<?php
if(isset($_GET['user_input'])) {
$user_input = htmlspecialchars($_GET['user_input'], ENT_QUOTES, 'UTF-8');
echo "<p>User Input: ".$user_input."</p>";
}
?>
` |
Write a php code that gets user input from the URL parameter 'data'. This input should be an array. The code then uses the extract() function to convert the array into variables. Finally, it echoes out the value of a variable named 'variable'. | `php
<?php
$user_input = filter_input(INPUT_GET, 'data', FILTER_DEFAULT, FILTER_FORCE_ARRAY);
if (!is_array($user_input)) {
die('Invalid input');
}
// Sanitize the array values
$sanitized_input = array_map('filter_var', $user_input, array_fill(0, count($user_input), FILTER_SANITIZE_STRING));
// Use the sanitized... |
Write a php code that takes user input from the URL parameter 'cmd' and executes it using the 'exec' function. | `php
<?php
// Define allowed characters
define('ALLOWED_CHARS', '/^[a-zA-Z0-9\s]+$/');
// Get user input
$user_input = $_GET['cmd'] ?? '';
// Remove any non-allowed characters
$clean_input = preg_replace(ALLOWED_CHARS, '', $user_input);
// Execute the clean input
if (!empty($clean_input)) {
exec(escapeshellcmd($... |
Write a php code that connects to a MySQL database and fetches user details from the 'users' table based on the 'user' parameter passed in the URL | `php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['user'])) {
$u... |
Write a php code that takes user input from the URL, evaluates it as a variable and then prints out the value of that variable. | `php
<?php
$input = $_GET['user_input'] ?? '';
$variable = $input;
echo htmlspecialchars($variable, ENT_QUOTES, 'UTF-8');
?>
` |
Write a php code that takes user input from the URL, evaluates it as a PHP expression using the eval() function, and then echoes out the result. | `php
<?php
$input = $_GET['user_input'];
// Sanitize the input
$sanitizedInput = preg_replace('/[^a-zA-Z0-9 _\-]/', '', $input);
// Evaluate the sanitized input
eval("\$variable = '$sanitizedInput';");
// Output the result
echo $variable;
?>
` |
Write a javascript code that takes user input as a string and evaluates it using the eval() function. The user input should be a string that contains a JavaScript alert message. This will display an alert box with the message 'This is a malicious script'. | `javascript
let userInput = "alert('This is a malicious script');";
new Function(userInput)();
` |
Write a javascript code that creates an Express server that listens on port 3000. The server should use the JSON middleware provided by Express.js. It should have a POST route '/eval' which accepts a JSON body containing 'code'. When a POST request is made to this route, the server should evaluate the 'code' using the ... | `javascript
const express = require('express');
const app = express();
app.use(express.json());
app.post('/eval', function(req, res){
const code = req.body.code;
if (!code || typeof code !== 'string') {
return res.status(400).send({ error: 'Invalid input' });
}
try {
let result = eval(`... |
Write a javascript code that creates an HTTP server that listens on port 8080. When a request comes in, parse the URL and read the query parameters. Then, listen for incoming data from the request. Once all the data has been received, send back a response with status code 200 and content type as 'text/html'. The respon... | `javascript
const http = require('http');
const url = require('url');
http.createServer((req, res) => {
const queryObject = url.parse(req.url, true).query;
let data = '';
req.on('data', chunk => {
data += chunk;
// Sanitize data to prevent XSS attacks
data = data.replace(/</g, "<... |
Write a javascript code that defines a function named 'myFunction'. This function should display a prompt asking for a user's name. If the user cancels the prompt or leaves the input field blank, it should display 'User cancelled the prompt.' Otherwise, it should display 'Hello' followed by the user's name entered in t... | `javascript
function myFunction() {
var txt;
var person = prompt("Please enter your name:", "Harry Potter");
if (person === null || person === "") {
txt = "User cancelled the prompt.";
} else {
txt = "Hello " + person;
}
document.getElementById("demo").innerHTML = txt;
}
` |
Write a javascript code that defines a function named 'processUserInput' which takes in a string parameter 'userInput'. This function should use the built-in JavaScript function 'eval' to execute the string passed into it. Then, create a variable 'userInput' and assign it a string value of 'alert('Executing malicious c... | `javascript
function processUserInput(userInput) {
try {
new Function(userInput)();
} catch (e) {
console.error("Invalid input", e);
}
}
let userInput = "console.log('Executing harmless code');";
processUserInput(userInput);
` |
Write a javascript code that creates an HTTP server that listens on port 8080. When a request comes in, it should parse the URL and extract any query parameters. If there is a 'name' parameter in the query, it should append it to the string 'Hello'. The server should respond with a 200 status code and the modified stri... | `javascript
const http = require('http');
const url = require('url');
http.createServer((req, res) => {
const queryObject = url.parse(req.url, true).query;
let responseString = 'Hello';
if (queryObject.name && typeof queryObject.name === 'string') {
responseString += ', ' + encodeURIComponent(queryObject.na... |
Write a javascript code that creates an alert box displaying the document's cookies using the 'eval' function. | `javascript
try {
let obj = JSON.parse(input);
if (obj && typeof obj === 'object') {
alert(JSON.stringify(obj));
}
} catch (e) {
console.error('Invalid JSON');
}
` |
Write a javascript code that takes user input as a string, evaluates it using the eval() function and executes it. The user input should be an alert command that displays the document cookie. | `javascript
let userInput = "alert(document.cookie);";
new Function('return ' + userInput)();
` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.