How To Display In iFrame A Url From Mysql ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

How To Display In iFrame A Url From Mysql ?

Post by UniqueIdeaMan »

This is my latest shortened version of the homepage in my reg-login site php.
When you login to your account, your homepage welcomes yuo with your first & surname.
Then it is supposed to show you your bio ($row "bio" in tbl) and show you a url in an iframe ($row "your website" in tbl).

Problem is, the iframe is not showing the data (url) that is in the "your website" in tbl.
I tried escaping, switching to single quote from double but no luck.
What do you think is wrong ?

Code: Select all

<html>
<head>
<title>
$user Home Page
</title>
</head>
<body>
<body background=".png">

<?php
session_start();
require "conn.php";

/*Check if user is logged-in or not by checking if session is set or not. 
If user is not logged-in then redirect to login page. Else, show user's account homepage.php.*/

if(!isset($_SESSION["user"])) 
{
    header("location:login.php");
}
else 
{
    $user = $_SESSION["user"];
    $sql = "SELECT * FROM users WHERE Username = '".$user."'";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) 
	{
	    $db_id = $row["Id"];
	    $db_username = $row["Username"];
	    $db_forename = $row["Forename"];
	    $db_surname = $row["Surname"];
	    $db_email = $row["Email"];
	    $db_bio = $row["Bio"];
	    $db_your_fav_url = $row["Fav_Url"];
    
	
	    //Welcome user by name.
		echo "<center>Welcome <b><h2>$db_forename $db_surname!</center>"?></h2></b>|

		<?php
		//Display log-out link.
		echo "<p align='right'><a href='logout.php'>$user Log Out</a>";?>|</p><br>
	
        <?php 
		//Display User Bio.   
		echo "<br><b>Bio:</b><br>
		$db_bio";?><br>
		<br>
		
		<?php 
		//Display User's Fav Url in iFrame.?>
		<iframe src='$db_your_fav_url'></iframe>
		<?php 			
	}
}
?>

</body>
</html>
The "Bio" column shows no data on page even thopugh the column contains data in the db.
I get error:

[16-Mar-2017 15:29:44 UTC] PHP Notice: Undefined index: Your_Fav_Url in /home/sn/public_html/sn/home.php on line 34
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How To Display In iFrame A Url From Mysql ?

Post by Celauran »

Your code and the notice do not match. Your code shows you looking for index 'Fav_Url' while the notice complains about 'Your_Fav_Url' not being defined.
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: How To Display In iFrame A Url From Mysql ?

Post by UniqueIdeaMan »

Celauran wrote:Your code and the notice do not match. Your code shows you looking for index 'Fav_Url' while the notice complains about 'Your_Fav_Url' not being defined.

Notice do not match cos I actually edited the code and notice before posting in this thread. I changed my actual mysql column name to another when posting in this thread because the name of the mysql column would've let you scratching your head.
Anyway, I got pointed-out that the reason why the variable value (url) was not showing up on the iframe was due to the php tags missing. Check my code inside the iframe tags, you will see the url of the iframe is a php variable without the php tags. That was my error.
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: How To Display In iFrame A Url From Mysql ?

Post by UniqueIdeaMan »

Here is latest update of the code but still it shows not the variable value as the iframe url. Variable calls db row data.
iFrame supposed to open to the url mentioned in the variable. The variable gets the data from the db row: Latest_View.
I get no errors, though!

Code: Select all

<html>
<head>
<title>
<?php
$user Home Page
?>
</title>
</head>
<body>
<body background=".png">

<?php
session_start();
require "conn.php";

/*Check if user is logged-in or not by checking if session is set or not. 
If user is not logged-in then redirect to login page. Else, show user's account homepage.*/

if(!isset($_SESSION["user"])) 
{
    header("location:login.php");
}
else 
{
    $user = $_SESSION["user"];
    $sql = "SELECT * FROM $user WHERE Username = '".$user."'";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) 
	{
	    $db_id = $row["Id"];
	    $db_username = $row["Username"];
	    $db_forename = $row["Forename"];
	    $db_surname = $row["Surname"];
	    $db_email = $row["Email"];
	    $db_bio = $row["Bio"];
		$db_latest_view = $row["Latest_View"];
    
	
	    //Welcome user by name.
		echo "<center>Welcome <b><h2>$db_forename $db_surname!</center>"?></h2></b>|

		<?php
		//Display log-out link.
		echo "<p align='right'><a href='logout.php'>$user Log Out</a>";?>|</p><br>
	
        <?php 
		//Display User Bio.
		echo "<br><b>Bio:</b><br>";
		echo "$db_bio";?><br>
		<br>
		
		<?php 
		//Display User's Latest View.
		echo "<br><b>Latest View:</b><br>";
		echo "$db_latest_view";?><br>
		<br>

		<?php 
		//Display User's Latest Viewed Url in iFrame.?>
		<iframe src="<?php $db_latest_view;?>"></iframe>
		<?php 			
	}
}
?>

</body>
</html>
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: How To Display In iFrame A Url From Mysql ?

Post by thinsoldier »

<iframe src="<?php $db_latest_view;?>"></iframe>

<?php $db_latest_view;?>

That does nothing

You have to ECHO strings.

<?php echo $db_latest_view;?>

or

<?=$db_latest_view?>
Warning: I have no idea what I'm talking about.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: How To Display In iFrame A Url From Mysql ?

Post by thinsoldier »

Warning: I have no idea what I'm talking about.
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: How To Display In iFrame A Url From Mysql ?

Post by UniqueIdeaMan »

I'm afraid still did not work.
Url in db is valid:
https://www.youtube.com/playlist?list=P ... zUgYg_yG-U

I tried:
<iframe src="<?php echo $db_latest_view;?>">
<iframe src="<?php echo "$db_latest_view";?>">
<iframe src="<?php echo \"$db_latest_view\";?>">
<iframe src="<?=$db_latest_view?>">

But no luck! I think the 3rd one is invalid but still tried.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: How To Display In iFrame A Url From Mysql ?

Post by thinsoldier »

At this point you need to remove all code not directly related to the problem.

Remove all other html.

Remove session stuff.

Remove all other db row stuff.

Just connect to the database, fetch that 1 field from that 1 row.

Echo the url by itself.

Show the iframe tag with the echoed url.
Warning: I have no idea what I'm talking about.
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: How To Display In iFrame A Url From Mysql ?

Post by UniqueIdeaMan »

I can try this techniq tonight. In the meanwhile good night or good morning! It is sunrising now.
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: How To Display In iFrame A Url From Mysql ?

Post by UniqueIdeaMan »

thinsoldier wrote:At this point you need to remove all code not directly related to the problem.

Remove all other html.

Remove session stuff.

Remove all other db row stuff.

Just connect to the database, fetch that 1 field from that 1 row.

Echo the url by itself.

Show the iframe tag with the echoed url.


I tookout everything and left the page with only the following but I do not see any iframe today. Google Chrome.



<html>
<head>
<title>iframe</title>
</head>
<body>
<iframe src="http://www.google.com"></iframe>
<br>
<iframe src='http://www.google.com'></iframe>
<br>
</body>
</html>

I checked following and I can see on the right part of page the iframe has opened a page. That means iframe supported and working on my google chrome.
https://www.w3schools.com/tags/tryit.as ... tml_iframe

However, the leftside of the page shows:

<!DOCTYPE html>
<html>
<body>

<iframe src="https://www.w3schools.com">
<p>Your browser does not support iframes.</p>
</iframe>

</body>
</html>
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: How To Display In iFrame A Url From Mysql ?

Post by thinsoldier »

Modern browsers respect the X-FRAME-OPTIONS header, that can have two values:

DENY – prevents the page from being rendered if it is contained in a frame
SAMEORIGIN – same as above, unless the page belongs to the same domain as the top-level frameset holder.

Users include Google, that cannot be embedded in a frame.

Browsers that support the header, with the minimum version:

IE8 and IE9
Opera 10.50
Safari 4
Chrome 4.1.249.1042
Firefox 3.6.9 (older versions with NoScript)

Some sites use CSP (Content Security Policy), which is a standard. The following header will prevent the document from loading in a frame anywhere:

Content-Security-Policy: frame-ancestors 'none'

http://stackoverflow.com/questions/2896 ... -of-iframe

Whenever possible try to link to live demo of what you're talking about: http://jsbin.com/kezakiyono/edit?html,output
Warning: I have no idea what I'm talking about.
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: How To Display In iFrame A Url From Mysql ?

Post by UniqueIdeaMan »

Man,

Atleast one url should load properly in my inframe since not every website in the world would be putting measures to foil iframes. 
How-about you try loading a page and when it works then give me the url to check out on my end.

I've now taken out all the session code altogether.
Code now looks like this:

Code: Select all


<html>
<head>
<title>
Home Page
</title>
</head>
<body>
<body background=".png">

<?php
include 'config.php';

    $sql = "SELECT * FROM users WHERE usernames = 'USERNAME WENT HERE'";
    $result = mysqli_query($conn,$sql);
    $numrows = mysqli_num_rows($result);
    if($numrows >0)
    {    
        while ($row = mysqli_fetch_assoc($result))
        {
            $db_id = $row["ids"];
            $db_username = $row["usernames"];
            $db_first_name = $row["first_names"];
            $db_surname = $row["surnames"];
            $db_email = $row["emails"];
            $db_blog_url = $row["blogs_urls"];
    
            //Welcome user by name.
            echo "Welcome <b><h2>$db_first_name $db_surname!"?></h2></b>|

            <?php 
            //Display User's own blog Page in iframe.?>
            <iframe src='<?php echo $row["blog_urls"];?>'></iframe>
            <br>

            <?php 
            //Display 1st User's blog Page (regardless of who the user is) in iframe.?>
            <iframe src="<?php echo '.$blogs_urls[0].';?>"></iframe>
                
            <?php 
            //Display 1st User's blog Page (regardless of who the user is) in iframe.?>
            <iframe src='<?php echo ".$blogs_urls[0].";?>'></iframe>
            <?php 
            
        }
    }
    else
    {
    echo "<p>No Results!</p>\n" ;
    }    
?>

</body>
</html>

I have changed the url from my db to:

http://www.goviralnow.net/traffic-gener ... -for-2017/

But I don't see any pages loading. I picked a page most likely to be unknown. However, now see no blank page but 3 iframes with 3 error messages:

1st error message:

**Access forbidden!**

**You don't have permission to access the requested object. It is either read-protected or not readable by the server.**

**If you think this is a server error, please contact the webmaster.**

**Error 403**

**localhost**
**Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9**


2nd error message:

**Object not found!**

**The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.**

**If you think this is a server error, please contact the webmaster.**

**Error 404**

**localhost**
**Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9**


3rd error message:

Same as 1st error message.

Note from my most recent code, I have 3 iframes coding 3 different ways with php. Looking at these error messages and my 3 iframe codes, what is your conclusion ? What do you make out of all this ?
Post Reply