Jump to content

Loginform with session and Ldap


Go to solution Solved by Barand,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/313891-loginform-with-session-and-ldap/
Share on other sites

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";
		}
?>

 

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

}

 

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";
		}
?>

 

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";
		}
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.