Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 41 additions & 38 deletions code-examples/protect-page-login/php/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,48 @@

<?php

class App {
// save the session to display it on the dashboard
private ?Ory\Client\Model\Session $session;
public ?Ory\Client\Api\FrontendApi $ory;
class App
{
// save the session to display it on the dashboard
private ?Ory\Client\Model\Session $session;
public ?Ory\Client\Api\FrontendApi $ory;

public function validateSession(){
$cookies = "";
// set the cookies on the ory client
foreach ($_COOKIE as $key=>$value) {
$cookies .= "$key=$value;";
}
public function validateSession()
{
$cookies = "";
// set the cookies on the ory client
foreach ($_COOKIE as $key => $value) {
$cookies .= "$key=$value;";
}

try {
// check if we have a session
$session = $this->ory->toSession("", $cookies);
if (! $session["active"]) throw new Exception('Session expired');
} catch (Exception $e) {
error_log('Exception when calling toSession: '.$e->getMessage());
// this will initialize a new login flow and Kratos will redirect the user to the login UI
header("Location: /.ory/self-service/login/browser", true, 303);
die();
}
$this->session = $session;
}
try {
// check if we have a session
$session = $this->ory->toSession("", $cookies);
if (! $session["active"]) throw new Exception('Session expired');
} catch (Exception $e) {
error_log('Exception when calling toSession: ' . $e->getMessage());
// this will initialize a new login flow and Kratos will redirect the user to the login UI
header("Location: /.ory/self-service/login/browser", true, 303);
die();
}
$this->session = $session;
}

public function printDashboard(){
echo '
<html lang="en">
<head>
<title>Ory Network secured Go web app</title>
</head>
<body>
<h1>Dashboard</h1>
<hr />
<h2>Your Session Data:</h2>
<pre><code>', json_encode($this->session, JSON_PRETTY_PRINT), '</code></pre>
</body>
</html>
';
}
}
public function printDashboard()
{
echo '
<html lang="en">
<head>
<title>Ory Network secured Go web app</title>
</head>
<body>
<h1>Dashboard</h1>
<hr />
<h2>Your Session Data:</h2>
<pre><code>', json_encode($this->session, JSON_PRETTY_PRINT), '</code></pre>
</body>
</html>
';
}
}
?>
48 changes: 28 additions & 20 deletions docs/getting-started/integrate-auth/05_php.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ To simplify URLs handling we install the [bramus/router](https://packagist.org/p
composer require bramus/router
```

## Create a new Ory project

```mdx-code-block
import CreateProject from '../_common/create-project.mdx'

<CreateProject />
```

## Install Ory CLI

To install Ory CLI follow [this guide](https://www.ory.sh/docs/guides/ory-cli-install-use)

### Why do I need the Ory CLI
Expand All @@ -52,36 +62,40 @@ import OryCLI from '../_common/ory-cli.mdx'

## Create an Entry Page

This is a working example of basic `index.php` script which creates an Ory client, registers new route for our Dashboard and makes
use of [Before Route Middlewares](https://github.com/bramus/router#before-router-middlewares) to validate if the user is allowed
to view the Dashboard.
Create a new file called `index.php` and paste the following code:

```mdx-code-block
import indexPHP from '!!raw-loader!../../../code-examples/protect-page-login/php/index.php'
import CodeBlock from '@theme/CodeBlock'

<CodeBlock language="php" title="index.php">{indexPHP}</CodeBlock>
<CodeBlock language="php" title="./index.php">{indexPHP}</CodeBlock>
```

## Validate and login

Next we will create handler which will check with your Ory project if the user has a valid session. Notice here that we are taking
the current `request` cookies and passing them along to the Ory client.
This entry script creates an Ory client, registers new route for our Dashboard and makes use of
[Before Route Middlewares](https://github.com/bramus/router#before-router-middlewares) to validate if the user is allowed to view
the Dashboard.

If the session is not valid the request is redirected to the Ory project for login. At this point we have not set up any custom UI
management and thus will be shown the Ory Account Experience login page.
We are yet to create an App class, let's do that now.

For the last part we need to add the Dashboard handler (the page we would like to protect) which will render an HTML with the
session data.
## Validate and login

This is accomplished by the simple `App` class stored in the `app.php` file:
Create a new file called `app.php` and paste the following code:

```mdx-code-block
import appPHP from '!!raw-loader!../../../code-examples/protect-page-login/php/app.php'

<CodeBlock language="php" title="app.php">{appPHP}</CodeBlock>
```

Create a handler that checks with your Ory project to determine if the user has a valid session. We take the current `request`
cookies and pass them to the Ory client.

This file validates the session and redirects to the login page if the session is invalid. If the session is not valid, the
request is redirected to the Ory project for login. At this stage, we have not set up any custom UI management, so the Ory Account
Experience login page will be displayed.

Finally, we added the Dashboard handler (the page we want to protect), which will render HTML with the session data.

## Run your app

Start your HTTP server and access the proxy URL
Expand All @@ -90,17 +104,11 @@ Start your HTTP server and access the proxy URL
php -S 127.0.0.1:3000
```

```mdx-code-block
import SdkEnvVar from '@site/src/components/SdkEnvVar'

<SdkEnvVar />
```

Next open a new terminal window and start the Ory Proxy. Upon first start, the Ory Proxy will ask you to log into your Ory Console
account.

```shell
ory proxy http://localhost:3000
ory proxy --project <PROJECT_ID> http://localhost:3000
```

To access the PHP app through the ORY proxy open [http://localhost:4000](http://localhost:4000) in your browser. You are presented
Expand Down
Loading