PHP CAS Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php

/**
 * Example for a simple cas 2.0 client
 * Taken from the official CAS example
 * PHP 5.4.0, CAS 1.3.0
 */

// Load the CAS lib
require_once 'CAS.php';

// Initialize phpCAS
phpCAS::client(CAS_VERSION_2_0, 'www.itsmetor.com', 443, '/cas');

// For quick testing you can disable SSL validation of the CAS server.
phpCAS::setNoCasServerValidation();

// force CAS authentication
// If CAS determines you are not authenticated, they will redirect you to their site and
// prompt for credentials
//
phpCAS::forceAuthentication();

// at this step, the user has been authenticated by the CAS server
// and the user's login name can be read with phpCAS::getUser().

// logout if desired, check for this before we print anything
if (isset($_REQUEST['logout'])) {
    phpCAS::logout();
}

// for this test, simply print that the authentication was successfull
echo "The user's login is: " . phpCAS::getUser();
echo "phpCAS version is: " . phpCAS::getVersion();

?>

<html>
  <head>
    <title>phpCAS simple client</title>
  </head>
  <body>
    <h1>Successfull Authentication!</h1>
    <p>The user's login is <b><?php echo phpCAS::getUser(); ?></b>.</p>
    <p>phpCAS version is <b><?php echo phpCAS::getVersion(); ?></b>.</p>
    <p><a href="?logout=">Logout</a></p>
  </body>
</html>

Leave a Reply