PHP SWITCH entering DEFAULT even if there is a valid CASE? - php

I have this SWITCH block:
switch ($_GET['page']) {
case "listaOferteTest":
include("php_views/lista_oferte_test.php");
break;
case "categorieOferte":
include("php_views/categorie_oferte.php");
break;
case "pagina":
include("php_views/pagina.php");
break;
default:
include("php_views/page_not_found_redirect.php");
break;
}
and some php links [they are dinamicaly generated, but i'll paste the html]:
<a href='/pagina/termeni-si-conditii/'>
Termeni si conditii
</a>
<a href='/pagina/informatii-utile/'>
Informatii utile
</a>
<a href='/pagina/contact/'>
Contact
</a>
I have a .htaccess where i treat the link like this:
RewriteRule (.*)/(.*)/(.*)/ index.php?page=$1&subPage=$2&subSubPage=$3 [L]
The problem: When I tested the links above I noticed a strange behavior - from about 10 clicks on random links one gets also the default. How is this possible? Thanks!

My first instinct would be to get the debugger out and step through the code so you can see exactly what's going on. If you don't have a debugger installed (why not? you should, it'll change your life!) or if you can't recreate the problem with a debugger given it's seemingly random nature, then next best thing would be to add some logging to the default block so you can work out why it's getting there. My guess would be that $glob['page'] doesn't match 'pagina' exactly; maybe it's got a leading/trailing space or slash, or maybe it's empty because there's a bug in how that value is being extracted. Something like Monolog would do the job in terms of logging.

Then u can use, if's instead of switch.
For ex.
if (isset($_GET['page'])) {
if($_GET['page']=='something') {...}
if($_GET['page']=='something2') {...}
...
}

Related

Sublime Text 3 custom PHP auto-completion does not work

Here's the default auto-completion for "switch":
switch (variable) {
case 'value':
# code...
break;
default:
# code...
break;
}
but I wish to turn it into just:
switch ()
{
case '':
break;
case '':
break;
}
because I don't like to modify "#code here..." every time.
I navigated to
"C:\Users\USER\AppData\Roaming\Sublime Text 2\Packages\PHP"
and opened "switch(-).sublime-snippet" and modified it into:
<snippet>
<content><![CDATA[switch ($0)
{
case '$0':
break;
case '$0':
break;
}]]></content>
<tabTrigger>switch</tabTrigger>
<scope>source.php</scope>
<description>switch …</description>
But nothing works.
Is there any syntax error? Or do I modify the wrong file?
If you tagged your question correctly, you modified the wrong file - you need to edit the Sublime Text 3 version. This is a bit more difficult to do directly, since the file is wrapped up in a .sublime-package zip archive. To get around this, install Package Control (if you haven't already), then install the PackageResourceViewer plugin. Open the Command Palette, type prv to bring up the PackageResourceViewer options, select Open Resource, then navigate down to PHP and select the switch(-).sublime-snippet option. Edit it to your liking, save it, and you should be all set.
You probably also want to set your tab stops differently. Try this instead:
<snippet>
<content><![CDATA[switch ($1)
{
case '$2':
$3
break;
case '$4':
$5
break;
${0:default:}
}]]></content>
<tabTrigger>switch</tabTrigger>
<scope>source.php</scope>
<description>switch …</description>
</snippet>
Now, you can tab through the different areas, filling in the info as you go, ending up at the bottom with a default option, that you can just hit Delete on to erase if you don't want it. With your original version, after typeing switchTab, you would have ended up with 3 different cursors, one at each of the $0 locations. Check out the snippets reference for more information.

Twilio - redirect to Twimlet in a PHP script

I have modified the Twilio call screening script so that it simultaneously tries several phones, and if someone preses a key to accept the call then it connects.
If this doesn't happen, or it times out, then it is sent back to a switch statement that decides what to do.
The issue I am having is being able to redirect to a Twimlet if the office is closed or there is no answer.
This si what i have so far:
switch ($DialCallStatus) {
case 'busy':
case 'no-answer':
case 'failed':
echo '<Response><Redirect>http://twimlets.com/voicemail?Email=al#domain.co.uk&Message=http://domain.co.uk/answerphone_open.mp3</Redirect></Response>';
break;
case 'closed':
echo '<Response><Redirect>http://twimlets.com/voicemail?Email=al#domain.co.uk&Message=http://domain.co.uk/answerphone_closed.mp3</Redirect></Response>';
break;
case 'completed':
echo '<Response><Hangup/></Response>';
break;
default:
echo '<Response>
//tries other phones
The switch seems to be working right as I can <say> different messages depending on the case, but the redirect doesn't work
I looked at the docs and as far as I va work out, it needs to be between tags. Wat am i doing wrong?
Thanks for your help!
PS. the URL works as its my fallback url.
PPS> Didn't post the full code here, but can do if its helpful.
edit: Errors
In my dashboard I get the following error:
<Response>
<Redirect>http://twimlets.com/voicemail?Email=al#dallasmatthews.co.uk&Message=http://dallasmatthews.co.uk/twilio/answerphone_closed.mp3</Redirect>
</Response>
Parse error found on line 1 of the Raw Response
My raw XML looks like this:
<Response><Redirect>http://twimlets.com/voicemail?Email=al#DOMAIN.co.uk&Message=http://DOMAIN.co.uk/twilio/answerphone_closed.mp3</Redirect></Response>
Did it!
I read over this stackexchange post and the clever author worked it out.
The & in the URL should have been & to make it work.
Hope this helps someone else

$_GET is not working

Creating a rating system and the info is not being transmitted through my $_GET variable. The code is below
if (isset($_GET['item'], $_GET['rating'])){
echo 'Works!';
}
The variable is being entered in this code below
<?php echo number_format(
$article['rating'],1); ?>
<div class = "rate">
Rate:
<?php
for ($x =1; $x<= $maximum_rating; $x++){
?>
<a href="prestige.php?item=<?php echo $article['id']; ?>&rating=<?php echo $x;?>">
<?php echo $x; ?></a>
<?php
}
?>
I am fairly new to programming so any ideas or tips would be greatly appreciated.
There are a couple of things you should do.
1.
Instead of
prestige.php?item=<?php echo $article['id']; ?>&rating=<?php echo $x;?>
Use
prestige.php?<?= http_build_query(array('item' => $article['id'], 'rating' => $x), '&') ?>
This will escape the parameters. Vars $article['id'] and $x could contain characters that break the HTML or URL.
2.
Look at the Net tab in your Firebug/Chrome dev toolbar. Are there any redirects? What headers are sent?
Also look at the address bar to see if prestige.php really is loaded with the GET parameters.
3.
Use a debug tool like XDebug to step through your code. You might have some code that resets the $_GET vars. Personally I use the IDE PHPed, but it's kinda expensive.
The code you posted works. So the snag must be in the code you did not post:
maybe the prestige.php page has a PHP error that prevents it from displaying anything; start with an empty file containing just <?php echo 'OK so far'; ?>.
maybe the page contains code (security checks, frameworks...) that kills $_GET. (reduce the page to a minimum working case, without include/requires)
maybe the page does work, but the output gets snarked by an untimely ob_end_clean() that was meant to "clean the page" before the real output started; (reduce the page to a minimum working case)
maybe the page works, the string 'Works' is there, but you can't see it due to HTML markup, CSS, or other rendering problems (check the page source)
the URL might be broken because the item code contains invalid URL characters (check what appears in the browser address bar)
there might be an URL rewrite scheme that interferes (check .htaccess and the server logs)
I just remembered something like this happening with international characters in the URL. Try with an ASCII-clean item code to see what happens.
Just to be sure: verify there is no auto_prepend'ed file which might interfere.
Then, it might also be more than one of the above acting together. Often when debugging one unintentionally breaks some code, and even after fixing the first bug, the code doesn't start working again - this doesn't mean the fix was invalid.
I'm sorry -- I'm at the end of my options. I really look forward to knowing what the reason was. (Usually the more explanations I amass, the more the real answer tends to be "none of the above". When it happens to me, sometimes I wonder whether to start to believe in gremlins :-( ).

Symfony 1.4 wrong URL routing

I have some actions defined like "action = '[module]/[action]'" & submit.
The result is '[currentUrl]/[module]/[action]' instead of '[module]/[action]', I can't figure out why, here's the code :
On the front :
<a href="<?php echo ( isset($disabledMajSuivi) && $disabledMajSuivi ? "#" : "javascript:showYesNoDialog('". addslashes(__('enquete.visu.msg.confirmation.majsuivi')). "', 'majSuivi')" ); ?>" >
<?php echo __('enquete.visu.majsuivi');?>
</a>
showYesNoDialog is a javascript function where first arg is the sentence displayed and second arg is callback function, so 'majSuivi' is called back and looks like this :
<script>
function majSuivi(response) {
if(response == 'yes') {
document.forms[0].action = "module_name/majsuivi";
document.forms[0].submit();
}
}
This has been debugged, the condition is true.
The action 'majSuivi' (that is big) ends like this :
$this->redirect('enquete/visu?enq_id='.$enq_id
. (!is_null($personneMorale) ? '&pm_id='.$personneMorale->getPmId() : '') );
But no action is executed because of the wrong url (so actualy this part of code is kind of useless).
So when the URL should be : http://[baseUrl]/index.php/module_name/majsuivi
It is instead : http://[baseUrl]/index.php/enquete/visu/enq_id/24/menu_id/module_name/majsuivi
Where /index.php/enquete/visu/enq_id/24/menu_id/ was the current url just before calling 'majSuivi action.
Each time I click on the "a href" button, it adds the "module_name" to the URL like following :
click -> http://[baseUrl]/index.php/enquete/visu/enq_id/24/menu_id/module_name/majsuivi
click2 -> http://[baseUrl]/index.php/enquete/visu/enq_id/24/menu_id/module_name/module_name/majsuivi
click3 -> http://[baseUrl]/index.php/enquete/visu/enq_id/24/menu_id/module_name/module_name/module_name/majsuivi
etc ...
I don't think it comes from the routing.yml config file, but if someone thinks it could, I'll write the content of it.
Thanks for your help
You should use something like this:
document.forms[0].action = "<?php echo url_for('module_name/majsuivi')?>";
or
document.forms[0].action = "/index.php/module_name/majsuivi";
or if you have this route in your routing.yml
document.forms[0].action = "<?php echo url_for('#your_route_name_from_yaml')?>";
Because 'module_name/majsuivi' is a relative path and will be concatenated with your current url without / at the beginning
Thanks a lot, these are very useful informations, espacially the third option I think.
Actualy I have different servers for testing before deploying. And I've observed two kind of behavior.
Behavior 1 : of every solutions I've tested, only document.forms[0].action = "/module_name/majsuivi"; is working.
Behavior 2 : of every solutions I've tested, none is working.
I've dug into php versions that were different from one testing server to one another, but putting two servers under the same PHP and Apache versions didn't make them have the same behavior about this issue.
As the deployment environment has behavior 1, I solved this issue for now by using the following javascript : document.forms[0].action = "/module_name/majsuivi";.
All of what I explained before was going on my local server, which match with behavior 1.
I've already tested the first option : document.forms[0].action = ""; which doesn't work on any of the servers.
The second one gives me http://[baseUrl]/index.php/enquete/visu/enq_id/24/index.php/module_name/majsuivi which is then not working, and I'm sure that if it doesn't work on local server, it won't on the others.
I have not tested the third solution as I kind of solved my issue for now, but it looks an effective solution, I think it should work. I'll update if I make use of it.
Thanks & Regards

zend routing works only in lowercase letters

I'm setting up routes in application.ini, so when I try to access /moved, it displays cont/move. It works but only if I type moved all lower letters exactly like it's setup on the first line. How can I make Moved or moVed or any other letter combination also work? Do I need to do it in Bootstrap to get finer control and how?
routes.test.route = moved
routes.test.defaults.controller = cont
routes.test.defaults.action = move
This is not a wise approach.
URLs are case sensitive for a reason. You will get duplicate content penalty from search engines. Users will be confused too.
However, you may create controller plugin to achieve this:
public function preDispatch()
{
$this->getRequest()->setControllerName(
strtolower($this->getRequest()->getControllerName());
)->setDispatched(false);
}
I've searched Google for a few minutes, and this page (http://joshribakoff.com/?p=29) covers a nice patch. This patch overrides the request object, instead of the dispatcher or the router.

Categories