Skip to content

Commit ed13e80

Browse files
docs: update php quickstart (#2057)
* docs: update php quickstart * chore: format
1 parent dcbd081 commit ed13e80

File tree

2 files changed

+69
-58
lines changed
  • code-examples/protect-page-login/php
  • docs/getting-started/integrate-auth

2 files changed

+69
-58
lines changed

code-examples/protect-page-login/php/app.php

+41-38
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,48 @@
33

44
<?php
55

6-
class App {
7-
// save the session to display it on the dashboard
8-
private ?Ory\Client\Model\Session $session;
9-
public ?Ory\Client\Api\FrontendApi $ory;
6+
class App
7+
{
8+
// save the session to display it on the dashboard
9+
private ?Ory\Client\Model\Session $session;
10+
public ?Ory\Client\Api\FrontendApi $ory;
1011

11-
public function validateSession(){
12-
$cookies = "";
13-
// set the cookies on the ory client
14-
foreach ($_COOKIE as $key=>$value) {
15-
$cookies .= "$key=$value;";
16-
}
12+
public function validateSession()
13+
{
14+
$cookies = "";
15+
// set the cookies on the ory client
16+
foreach ($_COOKIE as $key => $value) {
17+
$cookies .= "$key=$value;";
18+
}
1719

18-
try {
19-
// check if we have a session
20-
$session = $this->ory->toSession("", $cookies);
21-
if (! $session["active"]) throw new Exception('Session expired');
22-
} catch (Exception $e) {
23-
error_log('Exception when calling toSession: '.$e->getMessage());
24-
// this will initialize a new login flow and Kratos will redirect the user to the login UI
25-
header("Location: /.ory/self-service/login/browser", true, 303);
26-
die();
27-
}
28-
$this->session = $session;
29-
}
20+
try {
21+
// check if we have a session
22+
$session = $this->ory->toSession("", $cookies);
23+
if (! $session["active"]) throw new Exception('Session expired');
24+
} catch (Exception $e) {
25+
error_log('Exception when calling toSession: ' . $e->getMessage());
26+
// this will initialize a new login flow and Kratos will redirect the user to the login UI
27+
header("Location: /.ory/self-service/login/browser", true, 303);
28+
die();
29+
}
30+
$this->session = $session;
31+
}
3032

31-
public function printDashboard(){
32-
echo '
33-
<html lang="en">
34-
<head>
35-
<title>Ory Network secured Go web app</title>
36-
</head>
37-
<body>
38-
<h1>Dashboard</h1>
39-
<hr />
40-
<h2>Your Session Data:</h2>
41-
<pre><code>', json_encode($this->session, JSON_PRETTY_PRINT), '</code></pre>
42-
</body>
43-
</html>
44-
';
45-
}
46-
}
33+
public function printDashboard()
34+
{
35+
echo '
36+
<html lang="en">
37+
<head>
38+
<title>Ory Network secured Go web app</title>
39+
</head>
40+
<body>
41+
<h1>Dashboard</h1>
42+
<hr />
43+
<h2>Your Session Data:</h2>
44+
<pre><code>', json_encode($this->session, JSON_PRETTY_PRINT), '</code></pre>
45+
</body>
46+
</html>
47+
';
48+
}
49+
}
4750
?>

docs/getting-started/integrate-auth/05_php.mdx

+28-20
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ To simplify URLs handling we install the [bramus/router](https://packagist.org/p
4040
composer require bramus/router
4141
```
4242

43+
## Create a new Ory project
44+
45+
```mdx-code-block
46+
import CreateProject from '../_common/create-project.mdx'
47+
48+
<CreateProject />
49+
```
50+
51+
## Install Ory CLI
52+
4353
To install Ory CLI follow [this guide](https://www.ory.sh/docs/guides/ory-cli-install-use)
4454

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

5363
## Create an Entry Page
5464

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

5967
```mdx-code-block
6068
import indexPHP from '!!raw-loader!../../../code-examples/protect-page-login/php/index.php'
6169
import CodeBlock from '@theme/CodeBlock'
6270
63-
<CodeBlock language="php" title="index.php">{indexPHP}</CodeBlock>
71+
<CodeBlock language="php" title="./index.php">{indexPHP}</CodeBlock>
6472
```
6573

66-
## Validate and login
67-
68-
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
69-
the current `request` cookies and passing them along to the Ory client.
74+
This entry script creates an Ory client, registers new route for our Dashboard and makes use of
75+
[Before Route Middlewares](https://github.com/bramus/router#before-router-middlewares) to validate if the user is allowed to view
76+
the Dashboard.
7077

71-
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
72-
management and thus will be shown the Ory Account Experience login page.
78+
We are yet to create an App class, let's do that now.
7379

74-
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
75-
session data.
80+
## Validate and login
7681

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

7984
```mdx-code-block
8085
import appPHP from '!!raw-loader!../../../code-examples/protect-page-login/php/app.php'
8186
8287
<CodeBlock language="php" title="app.php">{appPHP}</CodeBlock>
8388
```
8489

90+
Create a handler that checks with your Ory project to determine if the user has a valid session. We take the current `request`
91+
cookies and pass them to the Ory client.
92+
93+
This file validates the session and redirects to the login page if the session is invalid. If the session is not valid, the
94+
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
95+
Experience login page will be displayed.
96+
97+
Finally, we added the Dashboard handler (the page we want to protect), which will render HTML with the session data.
98+
8599
## Run your app
86100

87101
Start your HTTP server and access the proxy URL
@@ -90,17 +104,11 @@ Start your HTTP server and access the proxy URL
90104
php -S 127.0.0.1:3000
91105
```
92106

93-
```mdx-code-block
94-
import SdkEnvVar from '@site/src/components/SdkEnvVar'
95-
96-
<SdkEnvVar />
97-
```
98-
99107
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
100108
account.
101109

102110
```shell
103-
ory proxy http://localhost:3000
111+
ory proxy --project <PROJECT_ID> http://localhost:3000
104112
```
105113

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

0 commit comments

Comments
 (0)