Correct syntax in Controller.php - php

In View.php file <?php echo $ticket->player_id ?> is working. But in controller.php it is not working. What is the correct syntax for Controller.php?
I am trying to use it here
'include_player_ids' => array('<?php echo $ticket->player_id ?>'),
The following is the sample code from controller file which are working properly
// Send Email
$email_template = $this->home_model->get_email_template_hook("ticket_reply", $lang);
if($email_template->num_rows() == 0) {
$this->template->error(lang("error_48"));
}
$email_template = $email_template->row();
if(isset($ticket->client_username)) {
$username = $ticket->client_username;
$email = $ticket->client_email;
$first_name = $ticket->first_name;
$last_name = $ticket->last_name;
} else {
$username = $ticket->guest_email;
$email = $ticket->guest_email;
$first_name = $ticket->guest_email;
$last_name = "";
}

In controller.php ,
$data['ticket'] = 'your data will be here';
In view.php,
'include_player_ids' => array($ticket->player_id),
Hope you understand.

Its already in php tags so you do not need to place <?php ?> again just assign variable directly
'include_player_ids' => array($ticket->player_id),

It should be
'include_player_ids' => array($ticket->player_id),
Please post the output of $ticket

Related

Look up MX server and display

I'm trying to make a script that receives "email" in GET and then displays MX server and i tired this code it did not work!
<?php
$email = $_GET['email'];
list($username,$domain) = split("#",$email);
echo getmxrr($domain)
?>
Found the answer
<?php
//$email = $_GET['email'];
//list($username,$domain) = split("#",$email);
//echo getmxrr($domain)
$email = $_GET['email'];
list($username,$domain) = split("#",$email);
$mxlist = array();
echo getmxrr($domain,$mxlist);
foreach($mxlist as $value){
echo $value . "<br>";
}
?>

array is showing empty not giving any value

I'm running function to check input empty but problem is that variable in array not working.
Here is my code:
$name = $email = $message = $result = "";
function has_presense($input){
if(empty($input)){
return $result = ucwords($input) ." is missing!";
}
}
if(isset($_POST['send'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$input = array($name,$email,$message);
foreach($input as $key => $value){
if(has_presense($key)){
$result = $value . "is missing";
}
}
}
Result:
Array ( [0] => [1] => [2] => )
it's showing empty array what is problem in it.
Description Make use of var_dump($_POST) or print_r($_POST) which will tell you what is in it if the form submission is successfully done.
The problem is that name, email, and message are empty.
Try:
var_dump($_POST);
to check if your the PHP is recieving everything.

Need to add session_start() in every method in controller

I use simple mvc in my project. This is my base Controller
<?php
class Controller {
function __construct() {
$this->view = new View();
}
public function loadModel($name, $modelPath = 'models/') {
$path = $modelPath . $name .'_model.php';
if(file_exists($path)) {
require $modelPath . $name .'_model.php';
$modelName = $name . '_Model';
$this->model = new $modelName();
}
}
}
this is base View class
<?php
class View {
function __construct(){
}
public function render($name) {
require 'views/layouts/header.php';
require 'views/' . $name . '.php';
require 'views/layouts/footer.php';
}
}
I added session_start(); on the top header.php
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
....
and this is how I display error from controller in the view
<?php
if (isset($_SESSION['errors']) && count($_SESSION['errors']) > 0) {
echo '<div class="row"><div class="col-lg-10 col-lg-push-2">';
echo '<div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">×</button><strong>Error!</strong><ul class="errors-list">';
foreach ($_SESSION['errors'] as $error) {
echo '<li>' . $error['message'] . '</li>';
}
echo '</ul></div></div></div>';
}
?>
but anyway I need to add session_start(); in the methods in controller, otherwise $_SESSION['errors'] isn't set
<?php
class User extends Controller {
function __construct(){
parent::__construct();
}
public function create() {
$name = $_POST['name'];
session_start();
unset($_SESSION['errors']);
unset($_SESSION['variables']);
$_SESSION['errors'] = array();
$_SESSION['variables'] = array();
$count = $this->model->checkIfUserExists($name);
if($count > 0) {
$_SESSION['errors'][] = array(
'message' => 'User Already exists',
);
$_SESSION['variables'] = array(
'name' => $_POST['name'],
'password' => $_POST['password'],
);
header('location: ' . URL . 'user/registration');
exit;
}
if(empty($_POST['name']) || empty($_POST['password'])) {
$_SESSION['errors'][] = array(
'message' => 'Fill required fields',
);
$_SESSION['variables'] = array(
'name' => $_POST['name'],
'password' => $_POST['password'],
);
header('location: ' . URL . 'user/registration');
exit;
}
$data = array();
$data['name'] = $_POST['name'];
$data['password'] = $_POST['password'];
$data['role'] = 2;
$userId = $this->model->create($data);
if($userId) {
$_SESSION['errors'] = $data['role'];
$_SESSION['loggedIn'] = true;
$_SESSION['userid'] = $userId;
header('location: ' . URL);
exit;
}
$_SESSION['errors'][] = array(
'message' => 'Error. Try again',
);
$_SESSION['variables'] = array(
'name' => $_POST['name'],
'password' => $_POST['password'],
);
header('location: ' . URL . 'user/registration');
exit;
}
}
and I don't understand why I need to add session_start(); in every method to make session works?
UPD
When I add check to method in controller
public function create() {
$name = $_POST['name'];
if(isset($_SESSION)) {
echo "yes";
} else {
echo "no";
}
die();
it displays 'no'
But when I tried to add session_start(); in construct
class User extends Controller {
function __construct(){
parent::__construct();
session_start();
}
I got an error
Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\test\views\layouts\header.php on line 1
Session needs to be initiated on every page call if you want to use it. This is a facility so you can have pages render faster if session is not needed and there is no overhead.
If you do not want to use session explicitly you can call that in your parent controller or bootstrap file which calls the class files.
On the other hand you can explicitly keep session on from your php.ini settings.

PHP Login using a txt file

hello I am working on a project where i have to create a php login using a text file. I have the basic code laid out but when I put a username and password on file and try signing in, it does not work. I could really use some advise. thank you. The code below is my login php file.
<?php
session_start();
$User = $_GET["user"];
$Pass = $_GET["password"];
if (!strpos($User,"#")) {
$User = $User . "#etown.edu";
}
$Validuser = false;
$_SESSION["user"] = $User;
$_SESSION["pass"] = $Pass;
$_SESSION["login"]= $Validuser;
?>
<!DOCTYPE html>
<html>
<body>
<?php
print "<h2>Welcome $User</h2>";
$infile = fopen("account.txt","r");
$entry = fgets($infile);
while (!feof($infile)) {
$array = explode(" ",$entry);
if ($array[0] == $User){
$name = $array[0];
$code = $array[1];
$code = substr($code,0,strlen($code)-1);
}
$entry = fgets($infile);
}
print "Name: $name <br/>";
print "pass on file: $code <br />";
fclose($infile);
if ($name==$User && $code==$Pass)
$Validuser = true;
$_SESSION["login"] = $Validuser;
print "That's All!<br/>";
if ($Validuser) {
print "Welcome valid user<br/>";
}
else {
print "You are not a valid user. Go become one first!";
print '<script type="text/javascript">';
print ' //document.location = "register.html";';
print '</script>';
}
?>
</body>
</html>
try adding this
$User = isset($_GET["user"]) ? $_GET["user"] : '';
$Pass = isset($_GET["user"]) ? $_GET["password"] : '';
$name ='';
$code='';
i'm assuming your txt file is like this
ja#etown.edu 12345
ja2#etown.edu 12345
ja3#etown.edu 12345

Checkbox value not displaying

The form inputs aren't displaying on the form.php page and negates my form validation. The error says undefined variable for all my variables on form.php. Please tell me what I have to edit in my code to make it display the form inputs on form.php. It works when I use it on the same page but I would rather it display on another page.
EDIT
Thanks so far but I can't get the value of the checkbox, the recipient(Administrator or Content Editor), to display it displays "Array" or "A".
contact.php
<?php
$errnam = "";
$errmail = "";
$errsub = "";
$errrec = "";
$hasErrors = false;
if(isset ($_POST['submitted'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$recipient = $_POST['recipient'];
$message = $_POST['message'];
if(preg_match("/^[\w\-'\s]/", $_POST['name'])){
$name = $_POST['name'];
}
else{
$errnam ='<strong>Please enter a name.</strong>';
$hasErrors = true;
}
if (preg_match("/^[\w.-_]+#[\w.-]+[A-Za-z]{2,6}$/i", $email)){
$email = $_POST['email'];
}
else{
$errmail = '<strong>Please enter a valid email.</strong>';
$hasErrors = true;
}
if(preg_match("/^[\w\-'\s]/", $_POST['subject'])){
$subject = $_POST['subject'];
}
else{
$errsub = "<strong>Please enter a subject.</strong>";
$hasErrors = true;
}
if (!empty($_POST['recipient'])) {
for ($i=0; $i < count($_POST['recipient']);$i++) {
$recipient = $_POST['recipient'];
}
}else{
$errrec = "<strong>Please select a recipient</strong>";
$hasErrors = true;
}
$message = $_POST['message'];
}
if ($hasErrors){
echo "<strong>Error! Please fix the errors as stated.</strong>";
}else{
header("Location: form.php?name=".$name."&email=".$email."&subject=".$subject. "&recipient=".$recipient. "&message=".$message);
exit();
}
?>
form.php
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$subject = $_GET['subject'];
$recipient = $_GET['recipient'];
$message = $_GET['message'];
echo "<h2>Thank You</h2>";
echo "<p>Thank you for your submission. Here is a copy of the details that you have sent.</p>";
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong>" .$recipient. "<br />";
echo "<strong>Message:</strong> <br /> " .$message;
?>
If you would like to transfer the data from contact.php to form.php you should use something like this:
contact.php
$data = urlencode(
serialize(
array(
"name" => $name,
"email" => $email,
"subject" => $subject,
"message" => $message)
));
header('Location: form.php?data=' . $data);
form.php
$data = unserialize(urldecode($_GET['data']));
$name = $data["name"];
$email = $data["email"];
$subject = $data["subject"];
$message = $data["message"];
This serializes the array of data from contact.php then URL encodes it and sends it as a GET variable to form.php. After, form.php URL decodes and unserializes the data for use.
The problem is when you header("Location:") to form.php, all the POST values are lost. You have to either resend them with the header, or modify them into GET and retrieve them again. It should be more efficient to have them both (contact.php AND form.php) in one page. That way, the form data only has to be sent once.
You could probably just send the POST values as GET over to form.php like this.
contact.php:
header("Location: form.php?name=".$name."&email=".$email."&subject=".$subject."&message=".$message);
form.php (to retrieve the values):
$name = $_GET['name'];
$email = $_GET['email'];
$message = $_GET['message'];
$subject = $_GET['subject'];
If you want to display form elements then you have to use this approach.
<form method="POST" action="contact.php">
Email<input type="text" name="email">
.......
.......
.......
// All elements
</form>
This may help you.
Give action in your form in contact.php
<form action="form.php">

Categories