How can I get a username from LDAP? - php

I'd like to ask you how can i dinamically get a username from ldap. As you can see below, i entered the username 'smith2'
$_SERVER["REMOTE_USER"] = 'smith2';
$param = $_SERVER["REMOTE_USER"]
And I can get his first name, like this:
$ldap1 = new ldapl;
$fname=$ldap1->getFname($param);
This is useful because I have some forms with some fields which are filled by default (name, first name, etc).
It must be dynamic. Each person has a PC, so the person Y should see his name, first name, etc The person X his name, first name, etc.
But i don't know how to get the username dinamically. Can you explain it to me?
Thanks

You can request the username via an AJAX call:
var remote_user = '';
$.get('path/to/server-side.php', function (response) {
remote_user = response;
/*you can populate your forms with information returned from your PHP script*/
});
This code will request information from a PHP script. Your PHP script could just output $_SERVER['REMOTE_USER'] and that will be the response variable in the AJAX callback.
A good way to communicate between PHP script and JavaScript is to use the PHP function json_encode() to output your server response. Then use the jQuery method $.getJSON() which will automatically parse the response into a JavaScript object that can be iterated through.
Documentation for $.get(): http://api.jquery.com/jquery.get
Documentation for json_encode(): http://www.php.net/json_encode

Related

jQuery $.post() a form with .serialize() / Where are my params in PHP?

I've got a html form and trying to send it with jQuery to a php script. In this script i am looking for the params.
$(document).ready(function(){
$("#myselect").change(function(e){
e.preventDefault();
$.post("mypath/toscript/",$("#form").serialize(), function(data){
alert(data);
});
});
});
I need all uri parts. So im fetching it with $_SERVER['REQUEST_URI'].
I am requesting something like this:
mypath/toscript/?p1=A&p2=B&p3=C
When i type this URI in the browser it works like it should.
But with the jQuery above i am getting with $_SERVER['REQUEST_URI'] only mypath/toscript/ without params.
Using $_POST shows me the prams p1=A&p2=B&p3=C
So why? Where is the difference when doing a post with $.post() to typing it directly
POST doesn't use the request URL to send the query string like GET does. POST instead sends the query string in the HTTP message body. When you type the query string into the browser, you are using method GET.
Here is an article from W3Schools about it.
That doesn't mean you should switch to using $.get necessarily though. There are drawbacks to using GET over POST, such as having a length limit or being less secure. Read the article to learn more.
To fix your code, you will have to choose which HTTP method suites your needs, and then align the jQuery and PHP code so that they both use the same method.
In your case try this will solve your problem ..
$.get("mypath/toscript/",$("#form").serialize(), function(data){
GET - Requests data from a specified resource..
POST - Submits data to be processed to a specified resource..
from w3school
You should use GET where you're doing a request which has no side effects, e.g. just fetching some info. This request can:
Be repeated without any problem - if the browser detects an error it
can silently retry Have its result cached by the browser Be cached by
a proxy
You have to provide serialize in formatted array way something like this
var data = $('#form').serializeArray();
$.post("mypath/toscript/",data , function(data){
alert(data);
});
Use parse_str to parse your serialized POST data into an array.
// $_POST['form'] : "num=12&obj=3&obs=text"
$form = array();
parse_str($_POST['form'], $form);
// you get :
$int_num = (int) $form['num']; // 12
$int_obj = (int) $form['obj']; // 3
$str_obs = $form['obs']; // "text"
A serialized form data is commonly used to update a database, so it's recommended to use POST vars.

ExtJS: How to store a user's login information only once?

I'm using Ext.Ajax.request() to hit a PHP page that gives me some user specific info as a part of a login process. Basically I want to store some variables (cookie, session information, etc) in ExtJS. I created a model with the needed fields and a store to keep it. Even though the authentication is done only once I want to ensure that on a given time there is only one copy of the user's info. I want to use that info to hit PHP again without having to authenticate again (cookies).
Here's how I fetch it and store it:
Ext.Ajax.request({
url: 'api/get.php',
params: {
user : usr,
pass : pass
},
success: function(response)
{
var text = response.responseText;
var jtext = Ext.JSON.decode(text);
var login = jtext.loginData;
model.set('username', login.username);
model.set('cookie', login.cookie);
model.set('password', login.password); // doing this feels soo wrong!
store.insert(0, model);
store.sync();
console.log(store.getCount());
}
});
What is the better way of doing it? Since I'm using an external (to the PHP) service currently I only check if the user/pass is correct and if it is, I pass it from the client-side to the PHP functions that do whatever they have to do with the external service, authenticating each time with them. Alternatively I can pass the cookie each time to the PHP that in turn passes it down. I can also map JS<->PHP cookies to PHP<->service cookies and that would be even better.
Cheers!
Ext.util.Cookies.set('myObj',Ext.encode(obj))
Use something like this and when you want to use it
id = (Ext.decode(Ext.util.Cookies.get('myObj'))).id

Filling data from one website into another website in php/javascript

I try to filling some data from one website into another website, which both website are not holding in the same server and I have no right to access into the back end of the other website.
For example:
I created a website that will collect the following data from the user
- name
- telephone number
- address
Then I have to pass those data (auto fill-in so that I do not have to manually enter the same data) into the other independent website for user information checking (t0 make sure that the address, telephone and address is the valid data).
Does anyone know how can I do it in php/javascript? Any example or tutorial can show?
I would use JSONP to move data between different domains and use JQuery's getJSON method to make a call to the server. The PHP file should return the data in proper format and the client should be able to read it using JQuery.
Here is a sample:
The server-side PHP code
<?php
header("content-type: application/json");
// Create a generic object.
// Assign it the property 'message' - the feedback message we want the user to see.
$rtnjsonobj->message = "You got an AJAX response via JSONP from another site!";
// Wrap and write a JSON-formatted object with a function call, using the supplied value of parm 'callback' in the URL:
echo $_GET['callback']. '('. json_encode($rtnjsonobj) . ')';
?>
Get data from client
$(document).ready(function() {
$("#jsonpbtn").click(function() {
var surl = "http://www.otherdomain.com/abovepage.php?callback=?";
$.getJSON(surl, function(rtndata) {
alert(rtndata.message);
});
});
});
What your asking is exactly cross-site scripting (XSS). All modern browsers will prevent you from executing any front-end (JS) script on any url which is not in the original domain.
You can try passing GET values into the page and if the devs built handles into their PHP for that, you might be able to populate the fields. I highly doubt this would work because of the massive security hole it would expose.
I don't know what it is your trying to do at the end of the day, but BE VERY CAREFUL. XSS is an exploit and there's a good chance you could get into trouble for it.

Javascript sending the wrong value

I am trying to send values to an mysql database using ajax from a form. I am selecting the value by taking the parent article of the form and then taking the child element with id="email" as you can see here...
var email = $(this).parent("article").children("#email").val() //gets the user's email
However when I send the data onto the php file for uploading to the mysql database it seems to be doing something wrong, and instead of the value typed in being stored a function (shown below) is being stored... What is going on here!?
function (a) {var c,d,e,g=this[0];{if(!!arguments....
you can try find() method:
The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well.
var email = $(this).parent("article").find("#email").val()
Seems a bit confusing. You're using #email, which implies an id, of which you can only have one on the page. jQuery will recognize the hashtag and default to using the native "getElementById" browser method.
So this:
var email = $(this).parent("article").children("#email").val()
Can be converted to this:
var email = $("#email").val()

Passing URL from php to javascript

I am reading a rss feed with php and creating html DOM from the same. Now I have a bunch of <li>'s with news feed. when a user clicks on a particular <li> I want to post certain data to another php file which performs some other function.
How I want the data is tricky. I have a URL for all the feed elements. When a user clicks on a particular feed, I need to retrieve the URL associated with that particular feed.
I want to run a $.click() function in which I am going to $.post to the next php script.
How do I get that URL without storing it in the HTML itself. I do not want to store the URL in the html document for security puposes.
I am new with PHP.
You will need to assign unique id (uid) to each list element that corresponds to the url. A good way of handling this would be a database. You send the list item's identifier, look up in the database the associated url, perform some magic, and send the response back to the client. You can use jQuery's data method that leverages the html5 data attribute to store the information. Here is the basic pseudocode.
html
<li class="feed" data-uid="12345">Go</li>
javascript
$('li.feed').click( function() {
$.post({ id: $(this).data('uid') }, function(data) {
//do something with data
});
});
php
$uid = $_POST['uid'];
//db lookup of url
//do something with url
//return data to page
you could encrypt the url being sent from the server and then decrypt when it's sent back from the client. Just use a simple salt encryption method. I assume you're going to use ajax or the like to post back from the client to the server in which case, this methodology would work for you.
alternatively, you could create an array and store the urls in a collection with an associative key that you send to the client, then look up the url on the server side by that key. You could also implement a database solution to do this same thing where the key is the ID in the db.
U have to encode the variable url...
PHP
var url = ...;
JavaScript
$(function (){ var d1 = <?php echo json_encode(array($url)); ?>;});

Categories