Creating profile using xml - php

i want to create profile, i tried this way but it didn't worked..
forum.php
<?php
session_start();
if(!file_exists('users/' . $username . '.xml')){
header('Location: index.php');
die;
}
?>
header.php
<?php
$xmlFile = new DOMDocument();
$xmlFile->load('users/'. $username .'.xml');
echo $xmlFile->getElementsByTagName('name')->item(0)->nodeValue;
?>
user.xml
<?xml version="1.0"?>
<user>
<name>yotam</name>
<lastname>dahan</lastname>
<password>73e872fc06e6de8562d1d040268dafc9</password>
<birthday>13.7.1999</birthday>
<country>Israel</country>
<gender>Male</gender>
<age>13</age>
<email>yotking789#walla.com</email>
<level>Admin</level>
</user>
I know the problem in $xmlFile->load('users/'. $username .'.xml'); here but i can't make specific file because i got diffrent members every time and i cannot create each one page.
Any other way to get $username define in xml file load?

Your code works as it should but what is the purpose here? If you are looking to store user details you should use a database such as MySQL which is then used to lookup this data instead of XML.
If you are getting errors then have you got PHP set to display errors? This might give you some idea what is going on.

Related

Redirect to XML file and back to PHP page possible?

I'm currently coding an ID filter, whenever a user submits an ID it will pick out all the orders and filters out all the ID's you DON'T want to see from the XML file.
a little preview - XML
<filter><!-- Copy filter-item and put the order-id in as the value to skip it-->
<filter_item>1138850639</filter_item><filter_item>1138371172</filter_item><filter_item>1137835945</filter_item></filter>
</root>
PHP
if (isset($_POST['load']))
{
$orderFilter = $_POST['orderItemFilter'];
$xml = simplexml_load_file("Config.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$itemsNode = $sxe->filter;
$itemsNode->addChild("filter_item", $orderFilter);
$sxe->asXML("Config.xml");
}
Now the problem is that the file itself does contain the ID, but the Config.xml HAS TO BE RUNNED to be able to display the final order page. Is there a way to redirect from an XML file to return to the previous page?
There are two mentioned to do this.
First you can do this in Javascript.
javascript:history.go(-1)
Second, you can do this in PHP, remember it is not ggood idea to because it will not work on HTTPS and headers can be hijacked and reirect to unwanted pages.
header('Location: ' . $_SERVER['HTTP_REFERER']);

How to append to current XML file? (Login System PHP/XML)

I've currently made a login system using xml and php. When registering a new user a new xml file gets created. Is there a way i can add the registered users to one xml file instead of creating a new xml every time?
REGISTER.PHP
<?php
$errors = array();
if(isset($_POST['login'])){
$username = preg_replace('/[^A-Za-z]/','',$_POST['username']);
$email = $_POST['email'];
$password = ($_POST['password']);
$c_password = $_POST['confirmPassword'];
if(file_exists('users/' . $username . '.xml'))
{
$errors[] = 'Username already exists';
}
//more validation here
if(count($errors) == 0){
$xml = new SimpleXMLElement('<user></user>');
$xml->addChild('password', md5($password));
$xml->addChild('email', $email);
$xml->asXML('users/'.$username.'.xml');
header('Location:login.php');
die;
}
}
XML FILE:
<user>
<password>827ccb0eea8a706c4c34a16891f84e7b</password>
<email>test#example.com</email>
</user>
You should change your xml at first
<users>
<user>
<email></email>
<password></password>
</user>
</users>
Then simply append new user to users element
Php eg.:
<?php
$users = new SimpleXMLElement('<users></users>');
$user = $users->addChild('user');
$user->addChild('email', 'test#test.com');
$user->addChild('password', '1111');
echo $users->asXml();
https://3v4l.org/AKdhJ
//edit
And as mentioned above, you should look for some different way for storing users info. MySQL, sqlite, those are much more suitable for this.
If you want to add user entries in the example you showed, you can just append them to the end of the file, right?
However, #Eakethet is correct that you should add a container (such as "users") around the entries, as a matter of good practice. Without that, there is not single "root", and so it is not a valid XML document (even if you provide a schema, which you haven't yet). Particularly with sensitive and important data, making sure your document is valid is pretty important (you wouldn't ship code with syntax errors in it, data is no less important).
If you add the container, you can't just append to the file any more, because you'd be adding after the final end-tag. But you can just backspace over it from the end, write the new entry, and then write a fresh copy of the final end-tag. See the more detailed discussion of this technique at Poor performance when writing large XML-based log file.
But as others have pointed out, user login data seems like it should go in a database, not a text file of any kind. Serial-searching a file for each user as they log in is going to get expensive fairly quickly, unless you know you're only going to have no more than, say, hundreds of users.

Parsing the Google Reader export

I am attempting to use SimpleXML to parse the xml file that is produced from the Google Reader export.
File example:
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>TheTechBox subscriptions in Google Reader</title>
</head>
<body>
<outline text="Engadget RSS Feed" title="Engadget RSS Feed"
type="rss" xmlUrl="http://www.engadget.com/rss.xml" htmlUrl="http://www.engadget.com"/>
<outline text="xkcd.com" title="xkcd.com" type="rss"
xmlUrl="http://xkcd.com/rss.xml" htmlUrl="http://xkcd.com/"/>
</body>
</opml>
This is what I have tried so far, the user uploads the file to this form and the form needs to loop through and extract the data.
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$import = new SimpleXMLElement($_FILES["file"]["name"]);
foreach($import->opml->body->outline[0] as $feed){
echo $feed["title"];
}
}
?>
Right now chrome produces a server error which indicates something is very wrong on the page, the file uploads OK so it appears to be the simpleXML part.
I am planning to do something more complex with the data later, I am currently trying to get it just to echo the data on the page (for demo purposes).
Any feedback would be greatly appreciated.
Here is the working code in the end
$import = simplexml_load_file($_FILES["file"]["tmp_name"]);
foreach($import->body->outline as $feed){
echo $feed["title"];
}
This will need adding to to parse all of the data but this works.
You've fallen into a classic trap when using SimpleXML: the first object you get when parsing a file or string is not an abstract "document" object, but the parent node. In this case, the parent node is <opml> ... </opml>, so $import in your sample code is that <opml> node. In other words, rather than $import->opml->body you just need to say $import->body.
There's another bug in your loop as well, which is that you are asking for the first <outline> element (->outline[0]) when what you want is to be looping over all of the elements (foreach( $whatever->outline as $feed )).

How to pass xml image in php page?

I'm new in php. I recently go through one login example in php (http://net.tutsplus.com/articles/news/build-a-login-and-registration-system-with-xml/) and implemented login page for my test site in local host.
But now i want to assing each user one image let say i have 3 user and i have 3 user xml under user folder and each xml should look something like this
<?xml version="1.0"?>
<user>
<password>some password will come here</password>
<email>user#user.com</email>
<img>images/mynewimage.jpg</img>
</user>
i added <img> node in xml but i don't know how to call that on php page. Here the biggest challenge is for me is to find out the current user on each page since i have 3-4 basic pages after login and i'm maintain his login on each page so i want his image also on all the page i given below code on each page to maintain session
<?php
session_start();
if(!file_exists('users/' . $_SESSION['username'] . '.xml')){
header('Location: login.php');
die;
}
?>
when i check with username <?php echo ucfirst($_SESSION['username']); ?> it shows me current user name but in same way i'm unable to call image next to username.
use simplexml_load_file()
$xml = simplexml_load_file('users/' . $_SESSION['username'] . '.xml');
echo $xml->img;
the first thing you have to do is parse your xml file.php have a function for this:
$xmls = simple_xml_load_file(your_file's_exact_path);
$image = $xml->img;
if you have many img elements in your xml file you have to use foreach loop for that like:
foreach($xmls as $xml)
{
$xml->img;
$image = implode(',',$$xml->img;);
}
this code parse you xml file and save all images to the variable $image. now you can use it as you want.make sure about the img element's path. just try with this. it will help you.
happy coding!

Using SimpleXML to get twitter avatar url (weird)

Im using simplexml to get the twitter profile avatar url from the xml status page.
this is the code im using
<?
$username = twitter;
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
echo $xml->user->profile_image_url;
?>
The xml page loads when i visit it, but for some reason nothing is being echoed. No errors. Nothing.
When i visit it in a browser, i get this:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>783214</id>
<name>Twitter</name>
<screen_name>twitter</screen_name>
<location>San Francisco, CA</location>
<description>Always wondering what everyone's doing.</description>
<profile_image_url>http://a1.twimg.com/profile_images/75075164/twitter_bird_profile_normal.png</profile_image_url>
<url>http://twitter.com</url>.....
(the rest is long and irrelevant to the question)
The data is there, why wont it echo?
After loading the XML document, the root element user is represented by the SimpleXMLElement object saved in $xml. Therefore $xml->user does not exist.
This one should work:
<?
$username = "twitter"; // <-- You did not use quotes here?! Typo?
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
echo $xml->profile_image_url; // <-- No $xml->user here!
?>
It is because the root element (in this case, <user>) is implied - you do not have to specify it.
Try this:
echo $xml->profile_image_url;
hey guys - tried out twivatar but I was getting an error 500 error????
anyway here is the code I ended up using - how it is helpful
<?php
if (isset($_REQUEST['user']))
//if "user" is filled out, get the user's twitter picture
{
$username = $_REQUEST['user'];
}else{
$username = 'aplusk';
//else lets use ashton kutcher's picture just for the F of it
} // <-- You did not use quotes here?! Typo?
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
$timage = $xml->profile_image_url; // <-- No $xml->user here!
header('location: ' . $timage);
?>
Now it is extremely easy to call this script from anywhere in your application simply print ...
<img src="http://mywebapp.org/timage.php?user=<php echo $data['twitter_screen_name'];?>" />
Twivatar
Previously, the easiest way to get the twitter avatar was to use Twivatar, which was built by Remy Sharp. However the service has since been shut down.
Using a third party for this is unnecessary.

Categories