473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

echoing a session variable from content within a database???

348 100+
Hello everyone. :)

I knew it wouldn't be very long before I was back with another question.

I just created a login box on my site and I am having an issue with trying to echo an error back to the user. The page content is in a database and I tried to put echo $_SESSION['error']; and all I get is "echo $_SESSION['error'];" instead of the error string.

Can someone please advise of a better way to do this? I want to keep the page in the db but yet I still have to let the user know that there was a problem logging them in if need be.

Thanks,

Frank
Jun 21 '08 #1
21 2055
Markus
6,050 Expert 4TB
You need to use delimiters.
<?php echo $_SESSION['error'];?>

Let me know if this helps.
Jun 21 '08 #2
fjm
348 100+
You need to use delimiters.
<?php echo $_SESSION['error'];?>

Let me know if this helps.
Hi and thanks for the help. I should have stated that in my post but I have tried it that way and still nothing. All it does is echo <?php echo $_SESSION['error'];?>

Could it be because the page in the database is loading before... or something. I'm out of answers..

I just tried that again and looked at the html source and can see:

<?php echo $_SESSION['error'];?>

Just figured I would add that. :) Thanks for the help.
Jun 21 '08 #3
fjm
348 100+
Does anyone have any ideas?
Jun 21 '08 #4
Atli
5,058 Expert 4TB
If the PHP code is being stored in a database, you will have to explicitly tell PHP to treat it as code, rather than just plain text.

Try using the eval function.
Jun 21 '08 #5
fjm
348 100+
If the PHP code is being stored in a database, you will have to explicitly tell PHP to treat it as code, rather than just plain text.

Try using the eval function.
I was very excited for a minute but it will just echo the eval function also. I just copied a small bit of code using the eval() function.

[PHP]<?php
$name = 'Joe';
$name2 = 'Jim';
$a = 'My friends are $name and $name2';
print $a . "<br>";
eval("\$a = \"$a\";");
print $a . "<br>";
?>[/PHP]

That is exactly what shows in the html source when I view it.

Should I be using the eval function somewhere else and not in my database content?
Jun 21 '08 #6
Atli
5,058 Expert 4TB
Yes, you should be using the eval function on the data from your database.
Like:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT pageContent FROM someTable");
  2. $row = mysql_fetch_assoc($result);
  3. eval($row['pageContent']);
  4.  
Where the "pageContent" column contains the entire HTML content, including the PHP statements.
Jun 21 '08 #7
Markus
6,050 Expert 4TB
Yes, you should be using the eval function on the data from your database.
Like:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT pageContent FROM someTable");
  2. $row = mysql_fetch_assoc($result);
  3. eval($row['pageContent']);
  4.  
Where the "pageContent" column contains the entire HTML content, including the PHP statements.
Ah!
So simple when you think about it.
Jun 22 '08 #8
fjm
348 100+
Well, today must not be my day... I am getting the following errors when I use eval() as suggested.

Array to string conversion ... on line 66
Parse error: syntax error, unexpected $end, .... eval()'d code on line 1
Here is how I am using it:
$content = $db->Row();
return eval($content);
Jun 22 '08 #9
Atli
5,058 Expert 4TB
What is the actual content of the $content variable?

I think the eval function assumes the content is PHP code, so it shouldn't start with <?php or end with ?>.

Try adding that to the string:
Expand|Select|Wrap|Line Numbers
  1. eval("?>". $content ."<?php");
  2.  
Jun 22 '08 #10
fjm
348 100+
Alti,

I no longer have the errors and actually have some good news. The html source shows:

Array<?php

I think we are getting closer. By the way, I completely cleared the db table and put in starting <?php tag and then removed it. Both ways did not work.

EDIT:
Forgot to answer your question. The only content in the table is pure html code. div tags, html and body tags
Jun 22 '08 #11
Atli
5,058 Expert 4TB
It shouldn't start with <?php
The eval function will consider the contents it is passed to be PHP code, so no tags are required.

If, however, the contents are not PHP code, but rather HTML, as it is with you. You must close the PHP tag before it can be used.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. $content = "
  2. <h1>Testing..</h1>
  3. <?php echo \"whott\"; ?>
  4. <h2>Daddaradaaa!!</h2>
  5. ";
  6.  
  7. eval("?>". $content ."<?php");
  8.  
Jun 22 '08 #12
fjm
348 100+
Atli, I totally cleared out the db table so there are no php tags or html tags in there now. (the table contents do not have any php tags what so ever anyhow; just thought I would throw that in)I an getting "unexpected $end" error with the code you last posted. I looked it over and I don't see any errors. ::confused::
Jun 22 '08 #13
fjm
348 100+
Atli,

this works:
$content = "dfsfg";
return eval("?>". $content ."<?php");

I can see "dfsfg" being echoed to the screen. Your code (<?php echo \"whott\"; ?>) was creating the error because it is being parsed inside of another php script (different script). This script processes the database content.

When I got rid of your start and end php tags in your <h2> elements, it worked.

I commented the $content = "dfsfg"; line and opened up my original $content variable and am now getting "Array" in place of a single string I placed in the db table (Hello World)
Jun 22 '08 #14
fjm
348 100+
I am going to put this one on hold for a while. I need to focus my attention on my OOP user class because hey, what good is an error code being echoed from the database content if the user can't even connect. :)
Jun 22 '08 #15
Atli
5,058 Expert 4TB
I am going to put this one on hold for a while. I need to focus my attention on my OOP user class because hey, what good is an error code being echoed from the database content if the user can't even connect. :)
Good point :)

Tho, for future reference, may I suggest another method.

Rather than storing the PHP code in you database, I would suggest adding a unique "tag", that can be replaced as you echo it.

Somewhat like:
Expand|Select|Wrap|Line Numbers
  1. $fromYourDB = "<b>Error: </b> :error:";
  2. echo str_replace(":error:", $_SESSION['error'], $fromYourDB);
  3.  
Is much easier than all that eval stuff.
Jun 22 '08 #16
fjm
348 100+
Atli, thank you! That is perfect. Why didn't I think of str_replace? I think I am just mentally drained. :)
Jun 22 '08 #17
Markus
6,050 Expert 4TB
Atli, thank you! That is perfect. Why didn't I think of str_replace? I think I am just mentally drained. :)
Adding to Atli's post:

If there is no error present, :error: will still be shown. Maybe use something like <error> which will be hidden. Although, this will not validate your HTML/CSS.
Jun 22 '08 #18
Atli
5,058 Expert 4TB
I wouldn't go putting invalid tags into your X/HTML. You never know what kind of problems this can cause in various browsers.

It would be better to just make sure that the :error: tag is replaced.
Just check if the session element is set. If it is not, just use an empty string instead.

Come to think about it... in the example I posted, the :error: string should always be replaced, even if the session element doesn't exist. It would just be replaced by... nothing, which works fine in this case.
It would produce a unidentified index notice tho.
Jun 23 '08 #19
fjm
348 100+
Adding to Atli's post:

If there is no error present, :error: will still be shown. Maybe use something like <error> which will be hidden. Although, this will not validate your HTML/CSS.
Hey Markus,

What I used was <!--ERROR-->

Not sure if that was the best way to go about it, but it is working like a charm.

Atli, what are your thoughts?
Jun 23 '08 #20
Atli
5,058 Expert 4TB
What you use as the keyword isn't really important, if you just make sure it gets replaced.

I try to use something that isn't like the markup, just to make sure I'm not replacing something I don't want to be replaced. Remember, the str_replace method replaces all occurrences of a string.

What if you actually had a HTML comment that said ERROR? Then you would be printing the error message there to :)
Jun 23 '08 #21
fjm
348 100+
What you use as the keyword isn't really important, if you just make sure it gets replaced.

I try to use something that isn't like the markup, just to make sure I'm not replacing something I don't want to be replaced. Remember, the str_replace method replaces all occurrences of a string.

What if you actually had a HTML comment that said ERROR? Then you would be printing the error message there to :)
Your right.. I suppose I could use :error: but I just didn't like the undefined index notices popping up. Using markup kinda eliminated that.
Jun 23 '08 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option...
2
by: Eric | last post by:
Hi, I've a problem with trying to retrieve a session variable in an include file. Basically, the main asp creates a session variable: <% Session("var1") = "Hello" %> And then when I click...
3
by: Nhi Lam | last post by:
Hi, I understand that there are 3 modes in which I can configure the SessionStateModule. What I need is an out of process Session State store with fail over support. The "SQL Server Mode" seems...
4
by: Stephen | last post by:
I have a .NET (1.1 framework) application that is losing a session variable on only a few PC's. The main page is loading up in a frame in a Portal application. On the Page_Load it stores an...
5
by: ASP.Confused | last post by:
As you can tell from my previous posts on this issue...I'm really confused :-/ I have a few ASP.NET web applications on my web host's "https" server. Our web host has a single "bin" folder for...
8
by: Steve Atkins | last post by:
I have an application where each user session opens and maintains a long-lived connection to the postgresql backend database. I need to keep a small amount of information (things like a unique...
4
by: Sarah Marriott | last post by:
Our website contains session variables that are used to validate if a user is logged in etc. We have found that these variables are randomly lost while navigating the website. We set up some...
6
by: Vyoma | last post by:
This is quite a bit of problem I am facing, and I cannot point exactly where I am going wrong. I have been lurking around at several forums with regard to login and user authentication scripts and...
5
by: Terry On Windigo | last post by:
I think I have figured out my problem but I don't know how to solve it. We are going to start using a forums package and do not want our users to have to login to both our site, and then again to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.