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!
Related
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']);
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 )).
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.
The situation is as follows. I have to send a defined XML to an url http:\\www.example.com:1234 with some variables that I have to previously define.
XML is like this:
<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
<Title51>Variable 2</Title51>
</Title5>
</Title1>
But, I want to define those variables (1, 2) within an html/php form and get method, so the user can introduce both variables and then click on the submit button from the form to send the XML to the previously URL.
Also, XML should have the "Content-Type","application/x-www-form-urlencoded" header.
Is this possible? I've tried to pass these variables directly to the XML and the best that I've come to is to showing the XML and not parsing the php strings.
Also, I've tried some scripts like simplexml from PHP classes, but with no luck so far.
1) To modify existing XML with new values . Try this
sample.xml :
<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
<Title51>Variable 2</Title51>
</Title5>
</Title1>
PHP :
$xml = simplexml_load_file("sample.xml");
$xml->Title3 = $_GET['t3']; // Updating <Title3></Title3> from GET method
$xml->Title5[0]->Title51 = $_GET['t5']; // Updating <Title51></Title51> from GET method
$xml->asXML('sample.xml'); // saving the xml file
2)To create new XML file (sample.xml) :
PHP:
$xml = new SimpleXMLElement("<Title1></Title1>");
$xml->Title2='Some Text';
$xml->Title3 = $_GET['t3'];
$xml->Title4='Some Text';
$xml->Title5[0]->Title51 = $_GET['t5'];
$xml->asXML('sample.xml'); // saving the xml file
I have showed you both possibilities mentioned in the comment . Use anyone which comforts you :)
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.