# Okta Sig Out / Logout in PHP or Javascript

**URL:** <https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806>\
**Category:** OAuth/OIDC\
**Tags:** php, javascript\
**Created:** [September 8, 2022, 1:42pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806 "2022-09-08T13:42:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![denis.futida](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@denis.futida](https://devforum.okta.com/u/denis.futida)\
**Post date:** [September 8, 2022, 1:42pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/1 "2022-09-08T13:42:04Z")

</div>

Hello guys,

Can someone send me code, preferably in PHP or Vanilla Javascript for Okta’s Sign Out / Logout, for me to insert in the code of my web application in PHP? I couldn’t implement any of the Sign Out code that is in the Okta Developer Quick Guide.

Following the documentation at:

> **[OpenID Connect & OAuth 2.0 API | Okta Developer](https://developer.okta.com/docs/reference/api/oidc/#logout)**
>
> Find information about the OAuth 2.0 and OpenID Connect endpoints that Okta exposes on its authorization servers.

When accessing Logout URL:  
GET https://${baseUrl}/logout?  
id\_token\_hint=${id\_token}&  
post\_logout\_redirect\_uri=${post\_logout\_redirect\_uri}&  
state=${state}

I have the 404 error page.

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [September 8, 2022, 4:25pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/2 "2022-09-08T16:25:29Z")

</div>

What are you using as a baseURL here? Are you following this [guidance](https://developer.okta.com/docs/reference/api/oidc/#composing-your-base-url) about how to determine the correct base url to use in your requests based on the authorization server you are using?

Aka, the baseURL should either be `https://{{subdomain}}.okta.com/oauth2` (if you are using the [Org Authorization Server](https://developer.okta.com/docs/concepts/auth-servers/#org-authorization-server)) or `https://{{subdomain}}.okta.com/oauth2/{{customAuthorizationServerId}}` (if you are using a [custom authorization server](https://developer.okta.com/docs/concepts/auth-servers/#custom-authorization-server)).

So depending on the issuer/authorization server you are using, the full logout request url (that you redirect to) will be `https://{{subdomain}}.okta.com/oauth2/v1/logout` or `https://{{subdomain}}.okta.com/oauth2/{{customAuthorizationServerId}}/v1/logout`

Remember that you can always get the full list of the /oauth endpoints for your server at its [metadata endpoint](https://developer.okta.com/docs/reference/api/oidc/#well-known-openid-configuration): `https://{{subdomain}}.okta.com/.well-known/openid-configuration` (Org Auth Server) or `https://{{subdomain}}.okta.com/oauth2/{{customAuthorizationServerId}}/.well-known/openid-configuration`

---

<div class="post-metadata">

**Author:** ![denis.futida](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@denis.futida](https://devforum.okta.com/u/denis.futida)\
**Post date:** [September 8, 2022, 5:45pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/4 "2022-09-08T17:45:38Z")

</div>

Hi @andrea,

I’m almost getting it with the URL

https://${subdomain}.okta.com/oauth2/v1/logout?id\_token\_hint=${okta\_id\_token}&post\_logout\_redirect\_uri=${REDIRECT\_URI}&state=${state}

Is this field via GET “id\_token\_hint” correct? I send the okta\_id\_token but I get a 400 error:  
Invalid request  
Your order resulted in an error. The id token is invalid.

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [September 8, 2022, 5:47pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/5 "2022-09-08T17:47:57Z")

</div>

You can’t make a curl request to [the /logout endpoint](https://developer.okta.com/docs/reference/api/oidc/#logout), you need to redirect to it in the browser.

Additionally, you do _not_ need to send the SSWS as auth for this endpoint. The only thing you need to pass it is the id\_token\_hint (which is the raw id token JWT string), e.g.: `https://${subdomain}.okta.com/oauth2/v1/logout?id_token_hint=eyj ***** `

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [September 8, 2022, 5:52pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/6 "2022-09-08T17:52:57Z")

</div>

> [@denis.futida](#):
>
> Is this field via GET “id\_token\_hint” correct? I send the okta\_id\_token but I get a 400 error:  
> Invalid request  
> Your order resulted in an error. The id token is invalid.

potential error causes are listed here: [Error conditions | /logout | Okta Developer](https://developer.okta.com/docs/reference/api/oidc/#error-conditions)

My main two guess right now

1. you are not sending the id\_token along in the same browser the user had their Okta session in
2. you are sending the id token to the wrong auth server. For example, if the `iss` in your ID token says `https:{{subdomain}}.okta.com/oauth2/default`, then your /logout endpoint will be `https://{subdomain}}.okta.com/oauth2/default/v1/logout`. The Default Authorization Server is NOT the same as the Org Authorization Server, which is highlighted in the docs I shared earlier

> [@andrea](#):
>
> [custom authorization server](https://developer.okta.com/docs/concepts/auth-servers/#custom-authorization-server)

> [@andrea](#):
>
> [Org Authorization Server](https://developer.okta.com/docs/concepts/auth-servers/#org-authorization-server)

---

<div class="post-metadata">

**Author:** ![denis.futida](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@denis.futida](https://devforum.okta.com/u/denis.futida)\
**Post date:** [September 8, 2022, 6:28pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/7 "2022-09-08T18:28:37Z")

</div>

@andrea,

It worked out! Thank you very much!

I used the URL https://{subdomain}}.okta.com/oauth2/default/v1/logout and logged out of Okta.

But it goes to the Okta login page, how do I redirect back to my application?

I’m trying https://{subdomain}}.okta.com/oauth2/default/v1/logout?id\_token\_hint=“.$\_SESSION[‘okta\_id\_token’].”&post\_logout\_redirect\_uri=".$\_ENV[‘OKTA\_OAUTH2\_REDIRECT\_URI’]; but no redirect.

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [September 8, 2022, 8:25pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/8 "2022-09-08T20:25:11Z")

</div>

Nice! Make sure that you add the URL you want users to get sent back to (the `post_logout_redirect_uri` parameter) as a"Sign-out redirect URI" for the application in the admin console. Okta won’t redirect the user back to a URI unless it _exactly_ matches one listed there.

---

<div class="post-metadata">

**Author:** ![denis.futida](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@denis.futida](https://devforum.okta.com/u/denis.futida)\
**Post date:** [September 8, 2022, 9:10pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/9 "2022-09-08T21:10:55Z")

</div>

@andrea ,

The logout URL is:  
https://{subdomain}}.okta.com/oauth2/default/v1/logout?id\_token\_hint=“.$\_SESSION[‘okta\_id\_token’].”&post\_logout\_redirect\_uri=[https://cmsdev.br.ibm.com/mdlStart/index.php/logout](https://cmsdev.br.ibm.com/mdlStart/index.php/logout)

In the Okta Admin Panel:  
Sign-out redirect URIs: [https://cmsdev.br.ibm.com/mdlStart/index.php/logout](https://cmsdev.br.ibm.com/mdlStart/index.php/logout)

But it still doesn’t redirect. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/oktadev/original/1X/589c4c5f80de422d01f47cee7185a7fb8227835c.png) [@system](https://devforum.okta.com/u/system)\
**Post date:** [September 9, 2022, 9:11pm UTC](https://devforum.okta.com/t/okta-sig-out-logout-in-php-or-javascript/21806/10 "2022-09-09T21:11:52Z")

</div>

Closed on September 9, 2022, 9:11pm UTC

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
