$response->redirect not works - php

I have an IVR that plays some audios for the user and then I need to redirect to another php file. The strange thing is that this redirect does not work.
NOTE 1: the redirect that does not work is the last one ($response->redirect("./chooseLicense.php?rs=$rs"))
NOTE 2: if I put it in if(!is_object($rs)) it works perfectly.
Can anyone tell me why this happens?
Snippet of my code:
$rs = json_decode(Curl::post(baseUrl . "users/auth/", $cpfOrCnpj, $password));
// logText('rs in login', $rs);
if(!is_object($rs)) {
$gather->play("https://digital-lock-api.azurewebsites.net/ura-hmg/assets/audios/licenseNotFound.mp3");
}
if($rs->licenses === []) {
$gather->play("https://digital-lock-api.azurewebsites.net/ura-hmg/assets/audios/licenseNotFound.mp3");
}
if(count($rs->licenses) == 1) {
$licenseId = $rs->licenses[0]->id;
$isBlocked = $rs->licenses[0]->blocked;
$token = $rs->auth->token;
$response->redirect("./waitForFlip.php?licenseId=$licenseId&isBlocked=$isBlocked&token=$token");
}
if(count($rs->licenses) > 1) {
$count = 0;
foreach($rs->licenses as $license) {
$count++;
$gather->play("https://digital-lock-api.azurewebsites.net/ura-hmg/assets/audios/chooseLicense$count.mp3");
$gather->play($license->nicknameAudioUrl);
}
$rs = json_encode($rs);
logText("rs encode", $rs);
$response->redirect("./chooseLicense.php?rs=$rs");
}

Related

else condition is working even if is true

I'm using codigniter 3.1.2 and using simple if condition in php but really confused when I checked my condition is true getting result from else condition as well and if as well mean getting data in from both cases.
public function editexperience($UserID = 0, $ID = 0) {
if ($this->Access->HaveAccess('Edit')) {
$Row = $this->Experience->Row($ID);
if ($ID > 0 && $Row){
print_r($Row);
Def(array(
'Update'=>'users/submit/updateexperience/'
),'Actions');
$User = $this->Model->Row(intval($UserID), '', 'CTRY.Currency');
$ID = intval($ID);
$Data['Row'] = $Row;
Def($Row,'Row');
$Data['Title'] = 'Edit Experience';
$Data['Fields'] = $this->GetExperienceFields($User->Currency, true);
$this->Main->SetPage('Helper/Edit', $Data, true);
} else {
TempNote('There is problem while getting request data!', 'Error');
redirect(URI(_E('{Page.List}'), true), 'refresh');
die();
}
}
}
In code I mentioned if I have Row then load template of edit.
Suppose:
I have Row and its loading edit template and also showing the error of else condition really confuse what is happening.
My PHP version is 5.6.31
TempNote Function
function TempNote($Message, $Type) {
//Prepare Data
$Current = array('Message'=>$Message,'Type'=>$Type);
//Get current instance
$Ins = & get_instance();
$Old = $Ins->Main->Messages;
// If another is already set and that should not be current
if (!empty($Old)) {
foreach ($Old as $Key=>$Now) {
if ($Now === $Current){
unset($Old[$Key]);
}
}
}
$Ins->Main->Messages[] = $Current;
$Ins->session->set_flashdata('Message', $Ins->Main->Messages);
}

Redirecting not working in PHP

I want to redirect to another page after inserting values in the database, but it is not redirecting. It remains on the same page. No error/warning is shown.
My code:
<?php
if (isset($_POST['btn-submit'])) {
$asth = $user->runQuery("SELECT MAX(Sr) as Sr FROM answer");
$asth->execute();
$aresult = $asth->fetch(PDO::FETCH_ASSOC);
$atf = ($aresult['Sr']);
$aI = "1";
$acid = $atf + $aI;
$arn = $fddate.$ydate.$mdate.$frandno.$acid;
$count = $i;
for ($j = 1; $j <= $count; $j++) {
$ufname = trim($_POST['radio'.$j]);
$Qrn = trim($_POST['qid'.$j]);
$istmt = $user->runQuery("INSERT INTO answer(ARN,Ans,QRN,SRN) VALUES(:user_arn, :user_crn, :user_qrn, :user_srn)");
$istmt->bindparam(":user_qrn",$Qrn);
$istmt->bindparam(":user_crn",$ufname);
$istmt->bindparam(":user_arn",$arn);
$istmt->bindparam(":user_srn",$srn);
$istmt->execute();
}
return $istmt;
$locationei = "result.php";
$locatione = $web.$locationei;
$user->redirect($locatione);
}
?>
In the starting of page:
require_once 'class.user.php';
$user = new USER();
In class.user.php:
public function redirect($url) {
header("Location: $url");
}
Your problem might lie in here:
return $istmt;
$locationei = "result.php";
$locatione = $web.$locationei;
$user->redirect($locatione);
Once you return a value, function stops execution and skips the rest of the code...
Also, you should ALWAYS exit() after redirection, as the redirection doesnt stop script execution and has been a source of problems in the past.
public function redirect($url)
{
header("Location: $url");
exit();
}

Laravel Session works on Chrome, Not on Iphone

I have created a system where users can search for payment solutions for their business here: http://104.236.45.72/
When you click an answer for a question, you'll notice that as you click through on chrome, you get a list at the top of previously answered questions.
When you view this same thing on your mobile device, you'll notice that the questions as the top only show ONE. It's like the session is not persisting on mobile. I have no idea why and I've tried changing the session driver from database to file and tried replacing all of the "$request->session->" references with "Session::", which leads to the same thing, it working on desktop and only showing one previous answer on mobile.
Here is the function being called when you answer a question on the fontend.
public function questions(Request $request, $id, $parentId, $question)
{
//Get the industry...
$data['industry'] = Industries::find($id);
//Get the parent level question and one child...
$data['parentQuestion'] = Questions::where('parentId', $parentId)->where('industryId', $id)->get();
if(count($data['parentQuestion']) > 0) {
foreach($data['parentQuestion'] as $pq) {
if($pq->parentId != 0) {
//How many steps are currently in the array?
if ($request->session()->has('lastSteps')) {
$hasLastSteps = TRUE;
}else {
$hasLastSteps = FALSE;
}
$stepCount = 1;
if($hasLastSteps == TRUE) {
$stepCount += count(Session('lastSteps'));
}
//Get the last steps ready...
$lsQuestion = Questions::find($question);
$lsAnswer = Questions::find($parentId);
$lsQ = array(
'unique' => uniqid(),
'questionId' => $question,
'question' => $lsQuestion->question,
'answer' => $lsAnswer->question,
'url' => '/'.$request->path(),
'stepCount' => $stepCount
);
$dup = 0;
//see if the session has a lastSteps array...
if ($hasLastSteps == TRUE) {
$temp = Session('lastSteps');
foreach($temp as $t) {
if($t['questionId'] == $question) {
$dup = 1;
}
}
}
//No duplicates exist, go ahead and add it to the session!
if($dup === 0) {
$request->session()->push('lastSteps', $lsQ);
}
}
//Grab its children...
if($pq->type === 'question') {
$data['children'] = Questions::where('industryId', $id)->where('parentId', $pq->id)->get();
}else {
//its an endpoint / answer!
}
}
}
//see if the session has a follow up array...
if ($request->session()->has('followUp')) {
$data['followUp'] = Session('followUp');
}else {
$data['followUp'] = array();
}
//see if the session has a lastSteps array...
if ($request->session()->has('lastSteps')) {
$data['lastSteps'] = Session('lastSteps');
}else {
$data['lastSteps'] = array();
}
return view('frontend.questions', $data);
}

Unable to add Codeigniter where in clause and where clause in mysql query

I am new to codeigniter and working on a project where I need to add search filters on a page. The way its been setup under applications/core there is a MY_Model.php file and here it the get_result function it.
function get_result() {
$this->db->from($this->tableName . ' ' . $this->alias);
$this->db->select($this->selectFields, FALSE);
for($j=0;$j<count($this->arrJoins);$j++){
$this->db->join($this->arrJoins[$j][0], $this->arrJoins[$j][1], $this->arrJoins[$j][2]);
}
$this->db->like($this->arrLike);
$this->db->or_like($this->arrOrLike);
if(isset($this->custLikeStr) && count($this->custLikeStr) > 0){
$this->db->where($this->custLikeStr);
}
$this->db->where($this->arrWhere);
$this->db->order_by($this->sortBy, $this->sortOrder);
$query = $this->db->get()->result();
//echo '<br>'.$this->db->last_query();
return $query;
}
There is a get_where function to build all where clauses
function get_where() {
foreach($this->arrWhereInputs as $arrWhereInput) {
if(isset($this->$arrWhereInput) && $this->$arrWhereInput != ''){
$this->arrWhere[$this->fieldAlias[$arrWhereInput].'.'.$arrWhereInput] = trim($this->$arrWhereInput);
}
}
return $this->arrWhere;
}
And in the model file for the application so under application/models here is there is a select function we need to call to get results.
function select(){
/* for search list */
$this->arrLikeInputs = array('stock_title', 'stock_id', 'price_band', );
$this->arrWhereInputs = array();
$this->arrWhere = array();
if ($this->input->post('record_per_page') != '') {
$records_per_page = $this->input->post('record_per_page');
} else {
$records_per_page = ROWS_PER_PAGE;
}
if (!(isset($this->record_per_page) && $this->record_per_page != '')) {
$this->record_per_page = ROWS_PER_PAGE;
}
$this->arrLike = $this->get_like();
$this->arrWhere = $this->get_where();
$this->arrWhere['SC.isDeleted'] = 0;
/* for search list */
if ($this->countQuery == 1) {
$this->db->from($this->tableName . ' ' . $this->alias);
$this->countSelectFields = 'COUNT(SC.stock_id) as cnt';
$result['count'] = $this->get_count();
}
$result['resultarr'] = $this->get_result();
$result['record_per_page'] = $records_per_page;
return $result;
}
Now I want to add where in to my query and I have tried a few ways but it just does not work.
I am getting confused on to where I am going wrong on such a simple thing.
So I have added a get_wherein function.
function get_wherein() {
foreach($this->arrWhereInInputs as $arrWhereInInput) {
if(isset($this->$arrWhereInInput) && $this->$arrWhereInInput != ''){
$this->arrWhereIn[$this->fieldAlias[$arrWhereInInput].'.'.$arrWhereInInput] = trim($this->$arrWhereInInput);
}
}
return $this->arrWhereIn;
}
So I added $this->arrWhereIn['Field'] = $arr which does not work and also tried to to call $this->db->where_in('field',$array) in the select function directly that does not work either. In both the cases it gets ignored and the query is build without the "and where in".
Where am I going wrong and how to build this query?

stopping duplicate urls in a form submission

I have a community site, where people can put up pages, and the url generates from their name. e.g. My Business, becomes my-business. I want to create a way, that if there is another My Business, it will check and make the url my-business-2, my-business-3, etc.
function check_url($url) {
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
if(mysqli_num_rows($qry)>0) {
$slugs = array();
while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
if(in_array($url, $slugs)) {
$max = 1;
while(in_array(($url . '-' . ++$max ), $slugs)) $url .= '-' . $max;
}
}
return $url;
}
This is my function, but this still wont work, as if there is a business called My Bus, it will make it my-bus-2, when it would have been unique at my-bus. I have experimented with other functions too, but this one is the closest I got. Can anyone tell me one that works perfectly?
So form my understanding, you are trying to avoid inserting/generating duplicate URL. Output will be something like this -
www.example.com/my-business
www.example.com/my-business-1
www.example.com/my-business-2
www.example.com/my-business-3
...
Try using this function -
function check_url($base_url, $new_url='', $num=0) {
if($new_url == '') {
$new_url = $base_url;
}
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url = '$new_url'");
if(mysqli_num_rows($qry) > 0) {
while($row = mysqli_fetch_array($qry)) {
$num++;
check_url($base_url, $base_url . '-' . $num, $num);
}
}
return $url;
}
Basically, this function will check the database until it finds a valid URL. Each time it will increment the number and recursively call the same function to generate new/valid URL.
Simple and basic!
#SpritsDracula has working code, but his function will query your database each time it executes. That being said, the recursion is a nice concept. Here is a piece of code that will save your the multiple database calls.
function check_url($url) {
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
if(mysqli_num_rows($qry)>0) {
$baseUrl = $url;
$slugs = [];
while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
$max = 1;
while(in_array($url, $slugs)) {
$url = $baseUrl . "-" . $max++;
}
}
return $url;
}

Categories