Setzi138 Posted October 7, 2021 Share Posted October 7, 2021 I want to create a login form that uses an Ldap for authentication after that the name should be shown at the top of the page for that i want to create a session this is the code <?php session_start(); ?> <?php $ldap_dn = "uid=".$_POST["username"].",dc=example,dc=com"; $ldap_password = $_POST["password"]; $ldap_con = ldap_connect("ldap.forumsys.com"); ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3); if(@ldap_bind($ldap_con,$ldap_dn,$ldap_password)) $_SESSION['username'] = ["username"]; echo $_SESSION['username']; else echo "Invalid Credential"; ?> but there are 2 warnings which i don't understand Warning: Undefined array key "username" in C:\xampp\htdocs\Kulinarik\ldap.php on line 15 Parse error: syntax error, unexpected token "else", expecting end of file in C:\xampp\htdocs\Kulinarik\ldap.php on line 17 i would really appreciate your help Thank you Quote Link to comment https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/ Share on other sites More sharing options...
Setzi138 Posted October 7, 2021 Author Share Posted October 7, 2021 Changed it a bit now it works better but another Error arised Warning: Array to string conversion in C:\xampp\htdocs\Kulinarik\ldap.php on line 16 Array <?php session_start(); ?> <?php $ldap_dn = "uid=".$_POST["username"].",dc=example,dc=com"; $ldap_password = $_POST["password"]; $ldap_con = ldap_connect("ldap.forumsys.com"); ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3); if(@ldap_bind($ldap_con,$ldap_dn,$ldap_password)) { $_SESSION['username'] = ["username"]; echo $_SESSION['username']; } else { echo "Invalid Credential"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/#findComment-1590745 Share on other sites More sharing options...
Barand Posted October 7, 2021 Share Posted October 7, 2021 If no login data has yet been sent to the form, there will be no POST data (such as when the page first loads). You need to check if data was posted if ($_SERVER['REQUEST_METHOD']=='POST') { // your above code goes here } Quote Link to comment https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/#findComment-1590746 Share on other sites More sharing options...
Solution Barand Posted October 7, 2021 Solution Share Posted October 7, 2021 3 minutes ago, Setzi138 said: $_SESSION['username'] = ["username"]; That line is setting the session value to an array. did you mean = $_POST['username'] ? Quote Link to comment https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/#findComment-1590747 Share on other sites More sharing options...
Setzi138 Posted October 7, 2021 Author Share Posted October 7, 2021 ahhh Thank you it works now <?php session_start(); ?> <?php $ldap_dn = "uid=".$_POST["username"].",dc=example,dc=com"; $ldap_password = $_POST["password"]; $ldap_con = ldap_connect("ldap.forumsys.com"); ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3); if(@ldap_bind($ldap_con,$ldap_dn,$ldap_password)) { $_SESSION['username'] = $_POST["username"]; echo($_SESSION['username']); } else { echo "Invalid Credential"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/#findComment-1590748 Share on other sites More sharing options...
Barand Posted October 7, 2021 Share Posted October 7, 2021 When I last worked with LDAP, I just used to interrogate the network LDAP servers to see who was logged in and what groups/depts etc they were in. ($_SERVER['login_user']) Quote Link to comment https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/#findComment-1590749 Share on other sites More sharing options...
Setzi138 Posted October 7, 2021 Author Share Posted October 7, 2021 just one other question is there a pure php way to redirect to the homepage after login? i tried with java but it didn#t worked out <?php session_start(); ?> <?php $ldap_dn = "uid=".$_POST["username"].",dc=example,dc=com"; $ldap_password = $_POST["password"]; $ldap_con = ldap_connect("ldap.forumsys.com"); ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3); if(@ldap_bind($ldap_con,$ldap_dn,$ldap_password)) { $_SESSION['username'] = $_POST["username"]; echo '<script type="text/javascript">window.open("C:\xampp\htdocs\Kulinarik\Startseite.php")</script>'; } else { echo "Invalid Credential"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/#findComment-1590751 Share on other sites More sharing options...
Barand Posted October 7, 2021 Share Posted October 7, 2021 <?php header("Location: Startseite.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/#findComment-1590754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.