Explode url page with switch case - php

Solved, this is my temporary solution for the 404 error page.
if (sizeof($url) != 1) {
header('Location: ../404');
}
else {
// nothing
}
default:
$page_name = 'Error';
$page_file = 'pages/errors/404.php';
break;

Change the code in your default: section to return HTTP 404 code and generate a proper template. See this question as reference.
PS. You seem to check only the first part of the URL, i.e. $_GET[0], but looks like you don't process any subsequent ($_GET[1],...) parts of it. In general I would advise you to either build your app on any mainstream framework with proper routing package in it or if you don't feel like using any fullstack PHP framework, you can install just any routing package via composer. E.g. symfony/routing

Related

Fixing php sizeof warning/error in WordPress plugin file with count()

I'm using the Hierarchical Pages plugin on a site and I notice that if I flip to a newer version of PHP, it errors out and it appears that using sizeof in this particualr instance is the culprit:
if (($args['show_home'] == 'yes') || (sizeof($page_info[$front_page]['children']))) {
$page_info[$front_page]['show'] = 1; // always show front page
}
To be honest, I'm not too familiar with this plugin, but after doing some digging it looks like it should be using something like count((array)...). So I updated it to this:
if (($args['show_home'] == 'yes') || (count((array)$page_info[$front_page]['children']))) {
$page_info[$front_page]['show'] = 1; // always show front page
}
And that seems to clear out the error, but I just want to make sure if that's a good or correct thing to implement and if I'm missing anything.
Modifying a contributed plugin is not suggested. If you ever update that plugin it may reintroduce the error. The best approach is to fork the plugin or submit a patch to the plugin developer.
Other than that sizeof() is just an alias of count() so if that resolves the error I would just issue roll with it.

Phalcon response object is not working

I cannot get the response object to work and I have tried many implementations referenced by the documentation. I don't have an external configuration for the response object, only what is in the code below.
When this code is run an empty page is displayed (there is no view linked with this action). I get no error messages in the browser / apache / php logs either.
I've checked the path to the controllers directory, which is located at:
"__ DIR __ . '/../controllers/'"
My code:
public function sendAction()
{
$this->response->redirect("contact/index");
}
If I place the following code at the top of the method, I see the expected page...
echo phpInfo();
die();
I have tried all combinations below:
$this->response->redirect("contact/index");
// Example shown in Phalcon Docs
$this->response->redirect("http://en.wikipedia.org", true);
return $this->response->redirect("http://en.wikipedia.org", true);
// Should navigate to the base URI
$this->response->redirect();
return $this->response->redirect();
// Creating a new instance of \Phalcon\Http\Response
$resp = new \Phalcon\Http\Response();
$resp->redirect("http://en.wikipedia.org", true);
I'm really not sure what else to try. I have scoured forums and documentation but haven't found anything that solves my issue.
Yes this question has been asked before but the answer didn't help!
I'm running Phalcon 2.0.1 | PHP 5.6.27 | Chrome v.54 | OSX 10.12
My opinion is that the code isn't wrong, but something must not be configured properly. I have tried installing php and Phalcon a couple of times, neither went smoothly and I downloaded a number of required packages and missing elements as I came across them trying to install. I then backed up my data and wiped my hard drive to start fresh, but the problem persists.
I had the same issue. Even if the official documentation makes examples like this
// Redirect to the default URI
$response->redirect();
// Redirect to the local base URI
$response->redirect("posts/index");
// Redirect to an external URL
$response->redirect("http://en.wikipedia.org", true);
// Redirect specifying the HTTP status code
$response->redirect("http://www.example.com/new-location", true, 301);
The only way it worked for me was returning the response send method. I hope it solves your problem too.
$this->response->redirect("http://en.wikipedia.org", true, 302);
return $this->response->send();

When using _redirect() in zend address bar not show the new url

First I want to clear something about me is that I am new to PHP and Zend.
I am developing a site using zend framework and here is a action call successAction....
public function successAction()
{
$responseParams= $this->_request->getParam('responseparams');
if (isset($responseParams)){
$pgResArray = explode("|", $responseParams);
if ($pgResArray[1] == 'SUCCESS') {
switch ($pgResArray[4]) {
case 'xyz':
$this->_redirect('/xyz');
break;
case 'pqr':
$this->_redirect('/pqr');
break;
case 'abc':
$this->_redirect('/abc');
break;
}
}
}
}
When the payment is successfully made by client the server throws the success flag to my site and I decided where to redirect now.
Every redirect url have some soap request.
My problem is that when I come back from payment gateway, address bar shows the url of payment gateway, but in the background all process done.
Redirect is working well but not shows on screen.
How to solve this...
Second thing what I want is that when I requesting to the soap server the screen will show processing image.
you may need to set the exit option on the action utility method _redirect(), otherwise you may wish to look into using the action helper redirector() as it has more available functionality.
Second thing what I want is that when I requesting to the soap server
the screen will show processing image.
This function is usually achieved using javascript, you may wish to explore the jquery javascript library as a bit of a javascript short cut.

What is the mechanism of "using $_GET instead of a new file"?

I saw some websites that (for example):
if you want to view your message box, then it is: example.com/file.php?action=pm and also if you want to send a message again the address and the file is the same, but the $_GET is different : example.com/file.php?action=sendpm
How does it work?
Is it just:
if ($_GET['action'] == "pm")
{
//lots of html code(divs, forms, etc) paste here for action=pm.
}
else
{
//lots of html code paste here for action=send
}
instead of having files : pm.php, send.php ... ?Or the mechanism is different?
I saw that SMF use this mechanism and also I saw something like this for facebook.com/?ref=something.
How does it work?
The easiest way is this:
$action = isset($_GET['action']) ? $_GET['action'] : 'default';
if(!in_array($action, array('list', 'of', 'allowed', 'pages')))
$action = 'default';
include($action . '.php');
Note that the validation step is incredibly important. Not properly validating the value leads to nasty security holes from users being able to read any files your script can access to users being able to execute arbitrary code.
Putting all code in a single file like you suggested in your question is a bad idea since it results in unreadable code. Especially when mixing PHP and HTML in the same file like your question suggested (tip: use a template engine!).
This is called routing and allows a single entry point to many functions within an single application.
Requests are taken, processed and routed to the correction function. This routing pattern fits well into the Model View Controller (MVC) pattern for organizing code.
Take a look at how the Zend Framework describes MVC and Routing:
http://framework.zend.com/manual/en/learning.quickstart.intro.html#learning.quickstart.intro.mvc
You can have a switch statement. For example
example.com/file.php?action=pm or example.com/file.php?action=sendpm
if (isset($_REQUEST['action'])) {
$param = $_REQUEST['action'];
switch($param) {
case "pm":
// Code goes here
break;
case "sendpm":
// Do something here
break;
default:
// handle the default case
break;
}
} else {
// Handle it here
}
Well there are even other ways to do it !!
Switch is one of the easiest way !!

How do I implement Direct Identity based OpenID authentication with Zend OpenID

I'm using the Zend framework and the openid selector from http://code.google.com/p/openid-selector/ - however I find I can't login using sites like Google and Yahoo as they use direct identity based login system whereby one is just redirected to a url as opposed to entering a unique url of their own for authentication.
I've checked out many options and hacks but none of them seem to work. How can i get this to work here btw - how is it implemented at stack overflow? I could really use all the help here guys..
Edit
Well the issue here is that from what I have noticed is that the Zend OpenID class doesn't support OpenID 2.0 the thing is that a typical open ID providor gives you a unique url such as your-name.openid-providor.com or openid-providor.com/your-name and the Zend OpenId class just parses through that url and then redirects you to the providor website where upon authentication you are redirected back.
In the case of Yahoo and google - you don't enter a unique url instead you are redirected to the providors login site and upon login and authentication you are redirected back - so basically whats happeining is that the zend_openID object when it parses to tell who the providor is it fails to tell from the general url itself. Like when you click on teh Google link it redirects you to https://www.google.com/accounts/o8/id
Its more an issue with the zend openid object here and there isn't any help on zend related forums - so I was wondering if someone had already hacked or had an alteration I could make to the class to accomplish this. Sorry if I'm missing something but I'm kinda new to this and programming with open ID and have just started to get my feet wet.
Thanks for the follow up - I did check into RPX a while back and they do have a php class but I wasnt able to check it out plus I really just want to for now get the code selector used as on stackoverflow to work with Yahoo and Google authentication. There has to be some kind of way to tweak the parsing which the Zend OpenID class uses as it runs a series of regular expression checks to make a discovery.
Little late to the game but I was able to get this working with some hacks I found around the interwebs.
First. Yahoo. To get Yahoo working all I had to do was change the JavaScript to use me.yahoo.com instead of just yahoo.com and it worked perfectly with the version of the Zend Framework I'm using. Unfortunately Google still wasn't, so some hacking was in order.
All of these changes go in Zend/OpenId/Consumer.php
First, in the _discovery method add the following on the series of preg_match checks that starts at around line 740.
} else if (preg_match('/<URI>([^<]+)<\/URI>/i', $response, $r)) {
$version = 2.0;
$server = $r[1];
I added this right before the return false; statement that's in the else {} block.
Second, in the _checkId method you'll need to add 3 new blocks (I haven't dug around enough to know what causes each of these three cases to be called, so I covered all to be on the safe side.
Inside the $version <= 2.0 block, you'll find an if/else if/else block. In the first if statement ($this->_session !== null) add this to the end:
if ($server == 'https://www.google.com/accounts/o8/ud') {
$this->_session->identity = 'http://specs.openid.net/auth/2.0/identifier_select';
$this->_session->claimed_id = 'http://specs.openid.net/auth/2.0/identifier_select';
}
In the else if (defined('SID') block add this to the end:
if ($server == 'https://www.google.com/accounts/o8/ud') {
$_SESSION['zend_openid']['identity'] = 'http://specs.openid.net/auth/2.0/identifier_select';
$_SESSION['zend_openid']['claimed_id'] = 'http://specs.openid.net/auth/2.0/identifier_select';
}
And then after the else block (so outside the if/else if/else block all together, but still inside the $version <= 2.0 block) add this:
if ($server == 'https://www.google.com/accounts/o8/ud') {
$params['openid.identity'] = 'http://specs.openid.net/auth/2.0/identifier_select';
$params['openid.claimed_id'] = 'http://specs.openid.net/auth/2.0/identifier_select';
}
Link to the bug in Zend Framework Issue Tracker
I need to use Google's OpenID stuff, and I tried Steven's code and couldn't get it to work as-is. I've made some modifications.
The _discovery change method is still the same:
Zend/OpenId/Consumer.php, line 765, add:
} else if (preg_match('/<URI>([^<]+)<\/URI>/i', $response, $r)) {
$version = 2.0;
$server = $r[1];
The rest is different, though:
Zend/OpenId/Consumer.php, line 859 (after making the above change), add:
if (stristr($server, 'https://www.google.com/') !== false) {
$id = 'http://specs.openid.net/auth/2.0/identifier_select';
$claimedId = 'http://specs.openid.net/auth/2.0/identifier_select';
}
This is right before:
$params['openid.identity'] = $id;
$params['openid.claimed_id'] = $claimedId;
And to get it to return the ID, once authorized:
Zend/Auth/Adapter/OpenId.php, line 278:
if(isset($_REQUEST['openid_identity']))
{
$this->_id = $_REQUEST['openid_identity'];
$id = $this->_id;
}
This is right before:
return new Zend_Auth_Result(
Zend_Auth_Result::SUCCESS,
$id,
array("Authentication successful"));
Note that I have not thoroughly tested this code. The code below is even more shakey.
I have spent more time and I've gotten it to work with my Google Apps domain with the following changes, in addition to the above:
Zend/OpenId/Consumer.php, line 734
$discovery_url = $id;
if(strpos($discovery_url, '/', strpos($discovery_url, '//')+2) !== false) {
$discovery_url = substr($discovery_url, 0, strpos($discovery_url, '/', strpos($discovery_url, '//')+2));
}
$discovery_url .= '/.well-known/host-meta';
$response = $this->_httpRequest($discovery_url, 'GET', array(), $status);
if ($status === 200 && is_string($response)) {
if (preg_match('/Link: <([^><]+)>/i', $response, $r)) {
$id = $r[1];
}
}
This is right after:
/* TODO: OpenID 2.0 (7.3) XRI and Yadis discovery */
I believe that was the only change I had to make. I'm pretty sure there's supposed to be some checking involved with the above for security reasons, but I haven't looked far enough into it to see what they would be.
Going over all the advice provided - I've decided to ditch using the zend_openid class [ sorry about that zend ] and instead I've switched to using JanRains OpenID library. Its taken a few hours to get it up and running with my project but atleast its working like a breeze. Had to make a lot of hacking and a bit of code spill over to get it working but its worth it.
I couldn't use any of Zend adapters with Zend-Auth to settle this new code library in as the library did the authentication on its own. SO I hacked and made a generic adapter that just returned a filled zend_result set to the Auth object thus I authenticate using my library and merely store the result in the Auth object pulling a bit of a fast one one the Zend-Auth object rather than have to rewrite my code again.
The library is available at http://openidenabled.com/php-openid/
Thanks for all the help guys.
I'm dealing with similar issues. I'm planning on using RPX now with Zend Framework. Maybe I'll write an adapter. Just to let you know.
Info: 'RPS now' provides an all-in-one interface and UI for user registration with
facebook
Google
Yahoo
mySpaceID
Windows LiveID
OpenID
aol
I'm pretty sure that Yahoo only works with OpenID 2.0. If you want to support Yahoo users, you're going to have to upgrade to a library with 2.0 support. That's going to be a matter of more than tweaking some parsing.
Did you check out the manual -- Zend_OpenId_Consumer basics? Check out 38.2.2 on that page and let me know if this helps, because it should.
Specifically, I don't know if Google offers OpenID. I know that Yahoo worked because I've tried it a while back.
Thanks for the information. I started by using JanRain's library, but I have problems with getting Simple Registration to work: I have not succeeded in getting any data that way. And there is no documentation on using Attribute Exchange. :(
So, I found and was trying Zend/OpenId, but had the same problem as you: no Yahoo!, Google and who knows what else support. Reading this, it seems I'll have to get back to JanRain; RPX is not an option in my case as it's a third party service.

Categories