Changing Page URL To PHP - php

Hi I want to convert pages URL like www.example.com/pictures.php?title=yyoy to something like www.example.com/page/yoyo.php
How can I do this?
www.example.com/pictures.php?title=yyoy these pages are generated when someone upload image to my website so I want to turn these types of URL to like above shown example.
Because these pages can't be index in search engines. Thanks
Picture.php
<?php
include("connection.php");
if(isset($_GET['title'])){
$page_id = $_GET['title'];
$select_query = "select * from save_data where Title='$page_id'";
$run_query = mysql_query($select_query);
while($row=mysql_fetch_array($run_query)){
$post_id = $row['ID'];
$post_title = $row['Title'];
$post_image = $row['Name'];
$page_title = $_GET['title'];
header('uploads/ ' . $page_title . '.php')
?>
<center>
<h2>
<a href="pictures.php?title=<?php echo $post_title; ?>">
<?php echo $post_title; ?>
</a></center>
</h2>
<center><img src="uploads/<?php echo $post_image; ?>" /></center>
<?php } }?>

I have not tested this, but you should use rules similar to the following in your .htaccess file:
RewriteEngine on
RewriteRule ^pictures.php?title=(.+)$ page/$1.php [R]
Note that, you will need mod_rewrite enabled for this to work in apache2

There's a good answer here: Masking $_GET variables of the URL when passing pages in PHP
That will tell you how to mask your URLs.
Alternatively, you could have these files uploaded to a directory in your file structure called "page/" Then you can access the files (as a download) inside the page directory with www.example.com/page/filename.php

Usually I would create an in-between page when posting from a form, but in your case your link looks like this:
www.example.com/pictures.php?title=yoyo
So it directs you to pictures.php with a variable 'title' in the URL.
So on your pictures.php page use:
$page_title = $_GET['title'];
And when you want to go to yoyo.php:
header('Location: $page_title.php')
From your edit this code will redirect the page but it will not display the last HTML:
<?php
include("connection.php");
if(isset($_GET['title'])){
$page_id = $_GET['title'];
$select_query = "select * from save_data where Title='$page_id'";
$run_query = mysqli_query($select_query);
while($row=mysqli_fetch_assoc($run_query)){
$post_id = $row['ID']; //Just make sure the values between [''] match -
$post_title = $row['Title']; //your columns in database.
$post_image = $row['Name'];
}
header('Location:uploads/$post_title.php');
?>
This will be left out:
<center>
<h2>
<a href="pictures.php?title=<?php echo $post_title; ?>">
<?php echo $post_title; ?>
</a></center>
</h2>
<center><img src="uploads/<?php echo $post_image; ?>" /></center>
<?php } }?>

URE This one
RewriteEngine on
RewriteRule ^pictures.php?title=(.+)$ page/$1.php [R, L]

You should use the .htaccess file. Add this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^picture/([a-zA-Z0-9]+).php$ pictures.php?title=$1
</IfModule>
If you're using a local server like WAMP, be sure to turn on de mod_rewrite or rewrite_module of apache and reboot the server.
You should also Google for Regular Expressions.
Let me know if it was useful.

Related

Image from MYSQL database (PHP, MySql)

I am trying to display a image URL from mysql database into my website. It's saved as URL in my database.
http://www.upload.ee/image/5697422/prague-1168302_1920.jpg
this is the url which should appear as $rida['pilt']
<?php
$paring = 'SELECT * FROM postitus ORDER BY id';
$valjund = $yhendus->query($paring);
while($rida = mysqli_fetch_assoc($valjund))
?>
<img class="img-thumbnail" alt="city" src="<?php echo $rida['pilt']; ?>" style="width:250px;height:200px" />
I guess in your code you're getting error undefined index as you're accessing your variable out of while loop. Try below code:
<?php
$paring = 'SELECT * FROM postitus ORDER BY id';
$valjund = $yhendus->query($paring);
while($rida = mysqli_fetch_assoc($valjund)) : ?>
<img class='img-thumbnail' alt='city' src='<?php echo $rida['pilt']; ?>' style="width:250px;height:200px" />
<?php endwhile; ?>
You should recheck $rida. After you use "echo var_dump($rida);", you will know all parameters in array. You can choose trust array

PHP for Site Title

I am trying to make a dynamic title using PHP on my website. My index.php open various client pages client.php. On these client pages on want the clients name to be shown as the page title.
I open the client page with the following PHP link.
<a class="iframe" href="client.php?id=<?php echo $rows['id']; ?>&Client Name=<?php echo $rows['client_name'] ?>">
And here is how I try to carry over the client name as title on client.php, but it just comes as a blank title...
<?php $client_name_title = $_GET['Client Name'];?>
<title><?php echo $client_name_title ?></title>
What am I doing wrong???
You need to pass the variable through the URL without spaces, like so:
<a class="iframe" href="client.php?id=<?php echo $rows['id']; ?>&client_name=<?php echo $rows['client_name'] ?>">
<?php
if(isset($_GET['client_name'])){
$client_name_title = $_GET['client_name'];
} else {
//client not set, revert to default behavior
}
?>

Set iframe location as the location in the $_GET element

How can I set an iframe src as the location in the $_GET properly.
Currently i'm trying this:
.htaccess
RewriteRule ^stream/(.*)$ index.php?p=stream&link=$1
stream.php/http://bbc.co.uk
<?php
echo '
<div class="container" style="height:1000px;">
<iframe src="'.$_GET['link'].'" frameborder="0" width="100%" height="100%"></iframe>
</div>
';
?>
This sets the iframe to go to 127.0.0.1/stream/http://bbc.co.uk when it should just go to bbc.co.uk
As I indicated in my comment, you need to rewrite php files to remove the .php so that the rule you posted matches, e.g.:
RewriteRule (\w+).php $1
RewriteRule ^stream/(.*)$ index.php?p=stream&link=$1
Which means your link will be re-written as so:
http://example.com/stream.php/http://bbc.co.uk
http://example.com/stream/http://bbc.co.uk
http://example.com/index.php?p=stream&link=http://bbc.co.uk
Then your php will output:
<div class="container" style="height:1000px;">
<iframe src="http://bbc.co.uk" frameborder="0" width="100%" height="100%"></iframe>
</div>
Which works as expected.
The url itself is not correct ie. it should have been in the form
stream.php?link=http://bbc.co.uk
Then you will get it in:
$_GET['link']
Other way would be store a mapping of the above get variable with the corresponding url:
ie.
var urlsMapping = {"bbc":"http://bbc.co.uk","toi":"http://timesofindia.indiatimes.com"}
Then make your url like:
stream.php?link=bbc
Set iframe src as
urlsMapping[<?=$_GET['link']?>]
Simply try this
RewriteRule ^stream/(.+)$ index.php?p=stream&link=$1 [L,QSA]
I hope this will hep you.
I fixed this using preg_replace
Probably not the best option, however it did work.
<?php
$link = preg_replace('~http:/~si', '', $_GET['link']);
echo '
<div class="container" style="height:1000px;">
<iframe src="http://'.$link.'" frameborder="0" width="100%" height="100%"></iframe>
</div>
';
?>

Google+ Plus API Define a specific image variable

I am trying to integrate the Google+ API on my website so that when a user submits approval via Oauth their Google+ activity feed will be displayed.
I have all of it working pretty much and am working on defining the variables that it will display for their feed.
Currently, this is what I have that IS working:
$activityMarkup = '';
foreach($activities['items'] as $activity) {
$url = filter_var($activity['url'], FILTER_VALIDATE_URL);
$title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$published = $activity['published'];
$totalItems = $activity['object']['plusoners']['totalItems'];
$activityMarkup .= "<div class='activity'>
<a href='".$url."' target='_blank'>$title</a>
<div>$content</div>
<div><br/><span>Date Published: </span>$published</div>
<div><br/><span>Total Likes: </span>$totalItems</div>
</div><br/><br>";
What I am trying to get to work is adding in the image that is associated with the post, namely the thumbnail.
I have tried different variations based on using existing code that works, but I just can't figure it out.
According to the Google API, this is the information for that image:
object.attachments[].image object The preview image for photos or videos.
object.attachments[].image.url string URL of the link.
Here's a link to all of the activities available if the above information isn't enough:
https://developers.google.com/+/api/latest/activities#object.originalContent
If someone can help me figure out how to define the variable for the image, that'd be awesome.
I figured it out. If anyone needs it, here's the code.
$image='';
if(isset($activity['object']['attachments'])){
foreach($activity['object']['attachments'] as $att){
if(isset($att['image']['url'])) {
$src = $att['image']['url'];
To call it out:
<img src='$src'>
<?php
$image='';
if(isset($activity['object']['attachments']))
{
foreach($activity['object']['attachments'] as $att)
{
if(isset($att['image']['url']))
{
$src = $att['image']['url'] ;
}
}
}
?>
<img src='<?php echo $src ?>'>
Works a Treat - https://developers.google.com/+/api/latest/activities#object.originalContent
<?php foreach($activities['items'] as $activity): ?>
<div class="activity" >
<div class="title" ><a href="<?php echo($activity['object']['url']) ; ?>" ><?php echo($activity['object']['content']); ?></a></div>
<p>Published at <?php echo($activity['published']); ?></p>
<p>
<?php echo($activity['object']['replies']['totalItems']); ?> Replys .
<?php echo($activity['object']['plusoners']['totalItems']); ?> Plusoners .
<?php echo($activity['object']['resharers']['totalItems']); ?> Reshares
</p>
</div>
<?php endforeach ?>
Another Way to add to the Feed is with replies, plusoners and reshares

Can't get image to display in dynamic pages

I can't seem to get my image to display properly. Previously, I have used the following code snippet and it worked perfectly.
catalog.php (worked perfectly):
<p class="image">
<a href="synopsis.php?id=<?php echo $row['id']; ?>">
<img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" />
</a>
</p>
synopsis.php (not displaying image at all):
<?php
$id = $_GET['id'];
...?>
<p class="image">
<img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>" width="250" height="400" />
<?php echo $row['synopsis']; ?>
</p>
where getimage.php:
<?php
$id = $_GET['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb", $link);
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo file_get_contents($row['dvdimage_path']);
?>
Any idea why can't I display this image?
EDIT 1:
So after debugging, I got an error message:
Undefined index: id in C:\xampp\htdocs\synopsis.php on line 106
so i went to add the following code into the php code just before echo $row['id']:
<p>getImage.php?id=<?php error_reporting(0); echo $row['id']; ?></p>
However,
the paragraph i got was just getImage.php?id=.
Then, i went into synopsis.php -> <img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>
and changed that into:
<img border="0" class="floatleft" src="getImage.php?id=2">
Again, same problem happens, where i can't get the specific image out.
I suspect something is wrong with my getimage.php file. However, this getimage.php file has been working fine for other pages when i use the snippet.
My requirements are very simple:
In catalog.php, i populate images and text from dvd database using a while loop. Then, each of these images has got their specific primary ID. when i click the the images, they will go to the link: synopsis.php?id="primaryid" Then, using this "primaryid" i should be able use getimage.php?"primaryid" to generate an image on synopsis.php page.
EDIT 2:
actually, i made a syntax error somewhere. So this line:
<img border="0" class="floatleft" src="getImage.php?id=2">
is working perfectly, this means the fault lies in somewhere that i cant echo 'id' out correctly.
EDIT 3:
I have included the links to the relevant source code:
catalog.php
synopsis.php
getimage.php
sortmenu.css
style.css
database in xml format
Questions to ask yourself:
Is it really required that you use a php script to mimic the image? If not, just use the image path.
Is there any output before the header(); function in the getimage.php file? Even just a space before the
Is the image actually a JPEG?
Are there any errors coming up when you go to getimage.php?id=ID in your browser?
In synopsis.php you are getting the id from the querystring but then trying to use a database value. I cant see all of your code so im posting solutions to cover two scenarios
src="getImage.php?id=<?php echo $id; ?>"
or
src="getImage.php?id=<?php echo $row[$id]; ?>"

Categories