I am getting syntax error, unexpected 'header' (T_STRING) in http://localhost:80/opt/lampp/htdocs/user_control.php [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying for user login and registration forms in Android. From past 5 days, I am trying it. I am not able to get it. Can anyone help me please? This is my code user_control.php. I am including the connection.php in user_control.php.
<? php
class user {
private $db;
private $connection;
/* The below one is the consttructor*/
function __construct() {
header('Content-Type: application/json'); /* used for including json format*/
require_once 'connection.php';
$this->db = new DB_Connection();
$this->connection = $this->db->get_connection();
}
public function does_user_exist($email,$password) {
$query = "Select * from userss where email ='$email' and password = '$password'";
$result = mysqli_query($this->connection,$query);
if(mysqli_num_rows($result) > 0){
$json['success'] = 'welcome'.$email;
echo json_encode($json);
mysqli_close($this->connection);
} else {
$query = " Insert into userss(email,password) values ('$email','$password')";
$is_inserted = mysqli_query($this->connection, $query);
if($is_inserted == 1) {
$json['success'] = 'Account created, welcome '.$email;
} else {
$json['error'] = 'Wrong password';
}
echo json_encode($json);
mysqli_close($this->connection);
}
}
}
$user = new user();
if(isset($_POST['email'],$_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
if(!empty($email) && !empty($password)) {
$encrypted_password = md5($password);
$user -> does_user_exist($email,$encrypted_password);
} else {
echo json_encode("You must fill both fields");
}
}
While I am running with postman software by Chrome I am getting the error as I am getting syntax error:
unexpected 'header' (T_STRING) in http://localhost:80/opt/lampp/htdocs/user_control.php

Remove the space in your line 1. Should looks like:
<?php

Try to comment:
//header('Content-Type: application/json'); /* used for including json format*/
Source: https://stackoverflow.com/a/20620606/2381906

Related

filter_var for FILTER_VALIDATE_EMAIL results in unknown error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can somebody please tell me what is wrong with the call to filter_var in validateEmail function below? If I comment the line hohum can be seen in FireBug or Chrome console, on the other hand Chrome reports something like: This request has no response. Coming from java background, I wonder if I need to include some library for the filter_var function?
<?php
function validateInput($data, $con) {
$data = trim($data);
$data = stripslashes($data);
$data = mysqli_real_escape_string($con, $data);
return $data;
}
function validateEmail($email, $con) {
$email = validateInput($email, $con);
if (filter_var($email, FILTER_VALIDATE_EMAIL) {
return $email;
} else {
return "";
}
}
function validateText($text, $con) {
$text = validateInput($text, $con);
if(preg_match('/[^a-zA-Z]/', $text) == 0) {
return $text;
} else {
return "";
}
}
$hostname="localhost";
$username="my_user";
$password="my_psswd";
$dbname="my_db";
$con = mysqli_connect($hostname, $username, $password, $dbname);
if (!$con) {
header('HTTP', true, 500);
echo('Could not connect: ' . mysql_error());
} else {
echo ("Hohum: ");
}
?>
You are missing a ),
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

error in mysql query string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
<form action = "index.php" method = "post">
username : <input type = "text" name = "uname" /><br>
password : <input type = "text" name = "pass" /><br>
submit : <input type = "submit" name = "submit" value = "submit" />
</form>
<?php
if(isset($_SESSION['id'])){echo $_SESSION['id'];}
if(isset($_POST['submit'])){
if ($_POST['submit'] == 'submit'){
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$db = "davidedwardcakes";
$connect = mysql_connect('localhost', 'root', 'wtfiwwu');
$db_connect = mysql_selectdb($db, $connect);
if(!$db_connect){echo 'no';}
$query = "SELECT * FROM `users` WHERE uname ='$uname' AND pass = '$pass'";
$result = mysql_query($query, $connect);
if(mysql_num_rows($result) > 0){//echo 'index failed'; var_dump($result);}
while($row = mysql_fetch_array($result)){echo $row['uname']
. "<br>";
session_start();
echo 'peruse';
$_SESSION['id'] = $row['id'];}}
else{echo 'lol'; var_dump($query);}}
Whenever I want to login, i get the error:
string 'SELECT * FROM users WHERE uname ='brown' AND pass = 'kenji'' (length=61)
meaning that theres a problem with my $query. If I remove the $pass query from $query it works fine but doesn't when it is included. Can anybody help please.
Let me convert your code to MySQLi at least. MySQL is already deprecated.
<?php
/* ESTABLISH CONNECTION */
$connect=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase"); /* REPLACE NECESSARY DATA */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
/* REPLACE THE NECESSARY POST DATA BELOW AND PRACTICE ESCAPING STRINGS BEFORE USING IT INTO A QUERY TO AVOID SOME SQL INJECTIONS */
$uname=mysqli_real_escape_string($connect,$_POST['username']);
$pass=mysqli_real_escape_string($connect,$_POST['password']);
$query = "SELECT * FROM `users` WHERE uname ='$uname' AND pass ='$pass'";
$result = mysqli_query($connect,$query); /* EXECUTE QUERY */
if(mysqli_num_rows($result)==0){
echo 'login failed';
var_dump($result);
}
else {
while($row = mysqli_fetch_array($result)){
echo $row['uname'];
} /* END OF WHILE LOOP */
echo 'Successfully Logged-in.';
var_dump($query);
} /* END OF ELSE */
?>

what`s wrong with this code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I write this but it doesnt work, can`t find the error.
this server side code get the the variable $cpu & $display and use it in select from the data base. when the variable is not important "*" will be sent.
<?php
if (isset($_REQUEST['action']))
{
$action = $_REQUEST['action'];
}
else
{
echo "Invalid Data";
exit;
}
if ($action == "read")
{
readData();
}
function connectToDatabase()
{
$connection = mysqli_connect("localhost", "root", "", "project_pro");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
return $connection;
}
function readData()
{
$connection = connectToDatabase();
$cpu = $_REQUEST['cpu'];
$display = $_REQUEST['display'];
This is the part that problem exists:
$sql = "Select * From phones WHERE";
if ($cpu == "*")
{
}
else
{
$sql+= " phone_cpu='$cpu'";
}
if ($display == "*")
{
}
else
{
$sql+= " AND phone_display='$display'";
}
$output = array();
while ($row = mysqli_fetch_array($result))
{
$record = array();
$record['phone_id'] = $row['phone_id'];
$record['phone_cpu'] = $row['phone_cpu'];
$output[] = $record;
}
echo json_encode($output);
mysqli_close($connection);
}
The concatenation operator in PHP is . not +. So change += to .=.

Parse error: syntax error, unexpected '}' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got this error
Parse error: syntax error, unexpected '}' in C:\wamp\www\widget_corp\public
\create_subject.php on line 24
when I clicked on my "Creae Subject" button.
I have tried to fix it all day but really can't find what the problem is.
<?php require_once ("../includes/session.php"); ?>
<?php require_once ("../includes/db_connection.php"); ?>
<?php require_once ("../includes/functions.php"); ?>
<?php require_once ("../includes/validation_function.php"); ?>
<?php
if (isset($_POST['submit'])) {
// Process the form
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int) $_POST["position"];
$visible = (int) $_POST["visible"];
// Validations
$required_fields = array("menu_name", "position", "visible");
validate_presences($required_fields);
$fields_with_max_lengths = array("menu_name" => 30);
validate_max_lengths($fields_with_max_lengths);
if (!empty($errors)) {
$_SESSION["errors"] = $errors;
redirect_to("new_subject.php")
} <-------- THIS IS ROW 24! ----------->
$query = "INSERT INTO subjects (";
$query .= " menu_name, position, visible";
$query .= ") VALUES (";
$query .= " '{$menu_name}, {$position}, {$visible}";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["message"] = "Subject created!";
redirect_to("manage_content.php");
} else {
// Failure
$_SESSION["message"] = "Subject creation failed";
redirect_to("new_subject.php");
}
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
?>
<!-- Close database connection -->
<?php if (isset($connection)) { mysqli_close($connection); } ?>
Missing semicolon on the line above the brace.
redirect_to("new_subject.php")
A lot of times you have to look at the line above the one given in the error to see what the problem is.
you have missed semicolon in this line:
redirect_to("new_subject.php");
try this:
if (!empty($errors)) {
$_SESSION["errors"] = $errors;
redirect_to("new_subject.php");
}

How to access returned variables from a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is the script that uses the "login" function and it returns two variables, how come in here when I try to take them from the sanitize function to the "login" function it doesn't work
index.php
<?php
if(!empty($_POST['submit']))
{
include("check.php");
$class = new check;
$user = $_POST['username'];
$pass = $_POST['password'];
$results = $class->sanitize($user, $pass);
$class->login($results[0], $results[1]);
//$class->login($user, $pass);
}
?>
check.php
function sanitize($string, $string2)
{
$stringh = htmlentities($string);
$string1h = htmlentities($string2);
$stringht = trim($stringh);
$string1ht = trim($string1h);
return $stringht;
return $string1ht;
}
function login($user, $pass)
{
$result = $this->link->query("SELECT * FROM `login` WHERE `username` = '".$user."'");
$numbers = mysqli_num_rows($result);
if($numbers != 0)
{
while($row = mysqli_fetch_assoc($result))
{
$dbuser = $row['username'];
$dbpass = $row['password'];
}
if($dbuser == $user && $dbpass == $pass)
{
echo "You have a match.";
}
}
}
You can only return 1 variable from a function. If you need two, wrap them in an array, giving you one object being returned.
return array($stringht, $string1ht);
However, a better solution may be to make your sanitize accept one input and have one output, and just call it twice, once for each string.

Categories