August 30, 2013

Shortened url redirect?

Huyimin’s Question:

I need help to get this done.

My webpages have formatted urls in this pattern: “http://qifu.us/index.php?page=item&id=4” for example – if there are more pages, only the last page id number will be different.

I want to get this part “index.php?page=item&id=” out, and shortened like “http://qifu.us/s4“, when a user input the shortened url into to address bar, it will be directed to the right page, which is the true url.

I am thinking to save the STATIC part “index.php?page=item&id=” into a string variable, and append the DYNAMIC page id – which is 4 in this example, then use Javascript or PHP to direct to the right page. But I don’t know how the steps, pls help. Thanks.

Actually an htaccess will be very good for this purpose.

For urls like http://qifu.us/s4 do the following: Create a file with name .htaccess and place at your root directory with the following content.

RewriteEngine On
RewriteRule ^s([^/]*)$ /index.php?page=item&id=$1 [L]
May 21, 2013

URL prevent href button action php

User222914’s Question:

Hi I have a href tag where I order an array values(up to down or down to up),so my problem is that when I click twice in the same button I have no action performed.That because of in my url I have something like that:
When I click for the 1st time on my button the action is done,but I click again on the same button nothing,but when I click ‘F5’ action the action is done:
Exemple in my URL when I click one time:

http://localhost/home/test.php?dir=down&keyword=1

The second time I click the URL remains the same but no action is done.it is done when I click on F5.

My hyperlink:

echo "<a href="$_SERVER[PHP_SELF]?dir=down&keyword=$keywordkey"><img src=web/images/remove.png /></a>";

How can I resolve this ?
Thank you!!!

After you click on the button for the first time, you give command to your script to sort it downwards.

Once this action is complete, you still have same command to provide to your script i.e. down

Since it is a same command going to the your code, is does exactly same thus, the results are also same.

June 3, 2012

Check if url contains parameters

Question by Puyol

Possible Duplicate:
keeping url parameters during pagination

I want to add a parameter to the current url with php, but how do I know if the url already contains a parameter?

Example:

foobar.com/foo/bar/index.php => foobar.com/foo/bar/index.php?myparameter=5
foobar.com/index.php?foo=7 => foobar.com/index.php?foo=7&myparameter=5

The main problem is that I don’t know if I need to add a “?”.

My code (found it somewhere, but it doesn’t work):

<?php   if(/?/.test(self.location.href)){ //if url contains ?
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&myparameter=5";
} else {
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?myparameter=5"; 
}?>

Answer by Starx

The URL parameters and received from a global variable called $_GET which is in fact an array. So, to know if a URL contains a parameter, you can use isset() function.

if (isset($_GET['yourparametername'])) {
    //The parameter you need is present
}

After wards, you can create separate array of such parameter you need to attach to a URL. LIke

if(isset($_GET['param1'])) {
    \The parameter you need is present
    $attachList['param1'] = $_GET['param1'];
}
if(isset($_GET['param2'])) {
    $attachList['param2'] = $_GET['param2];
}

Now, to know whether or not, you need a ? symbol, just count this array

if(count($attachList)) {
    $link .= "?";
    // and so on
}

Update:

To know if any parameter is set, just count the $_GET

if(count($_GET)) {
     //some parameters are set
}
May 4, 2012

Get part of URL with PHP

Question by user1069269

I have a site and I would need to get one pages URL with PHP. The URL might be something www.mydomain.com/thestringineed/ or it can www.mydomain.com/thestringineed?data=1 or it can be www.mydomain.com/ss/thestringineed

So it’s always the last string but I dont want to get anything after ?

Answer by TecBrat

You will use the parse_url function, then look at the path portion of the return.
like this:

$url='www.mydomain.com/thestringineed?data=1';
$components=parse_url($url);

$mystring= end(explode('/',$components['path']));

Answer by Starx

parse_url() is the function you are looking for. The exact part you want, can be received through PHP_URL_PATH

$url = 'http://php.net/manual/en/function.parse-url.php';
echo parse_url($url, PHP_URL_PATH);
April 7, 2012

Hide "www" text in URL bar (firefox)

Question by Futur Fusionneur

I was wondering if it is possible to hide the “www” text in the URL bar (only in Firefox) using CSS in Stylish addon or/and Java in Greasemonkey.

I want this to make Firefox even more compact.


This is some CSS code that i found for URL bar in firefox that will modify the text size using Stylish. Hope it can help.

@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");

.urlbar-input-box,
.searchbar-textbox {
 font-size: 11px !important;
}

Update

I don’t want to remove the “www”, I just want to hide it from the url bar.

Answer by Starx

You have to use .htaccess for this

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Replace example.com with your domain name.

March 20, 2012

How to get folder root from url?

Question by Earvin Bryan S. Co

How do you get the root folder from a php file?

For example

URL: http://localhost/project_name/

Result: return “project_name”

Answer by Starx

Use the parse_url() function for this

$url = $_SERVER['REQUEST_URI']);
$urlParse = parse_url($url);

echo $urlParse['hostname'];

However, this will only work, if you are using a webserver, for these type of url

http://www.mydomain.com

If you want this to work on a localhost, add some few lines

$url = $_SERVER['REQUEST_URI']);
$urlParse = parse_url($url);

$path = explode('/',$urlParse ['path']);
echo $path[1]; //gives project_name in your case
March 3, 2012

Folder with Get value

Question by Smile Applications

Could someone with more experience than me explain how it works the link (for example):

http://www.facebook.com/zuck  

I think it’s the same thing of this

http://www.facebook.com/profile.php?id=4

I imagine that “zuck” is a GET type string but I don’t understand how I can do the same thing.

Thank You very much

Answer by ilya iz

.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?id=$1 [L]

Answer by Starx

Actually, i am not sure about it, but by the way I see it, facebook probably uses both ways to get to a profile

I quick .htaccess to make sure all the request arrive on same page.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?input=$1 [L]

Now, in the profile.php, it should do a simple check like

$input = $_GET['input'];

if(is_string($input)) {
 // then retrieve profile id, based on the string
}
//now either way you have an unique identifier at last
//
//
// use your logic further more
December 29, 2011

how to know how many pages a particular user visited in a particular session

Question by Anup Kumar

i am working on a e-commerce website, i want to monitor how many pages a users visited in a particular session. there are only registered users of the site.
can any one tell me how to record the URL of the user of a particular session,
i am using php and mysql

Answer by Starx

There are more than one way to do this. Here is one quick walkthrough:

  1. Create a table with two fields (‘session_id’, ‘url’);

  2. Whenever the site loads, read the sessionid using session_id(), and use this value to store the url of the page in a table for every pages the people visit.

  3. Once a page loads, read the URL, through $_SERVER['REQUEST_URI']; and then use it the value of session_id() and store the records.

After you implement this simple technique. you can see how many people see the pages, using a simple query

SELECT count(*) FROM <YOURSESSIONTABLE> GROUP BY `<session_id_field>`

P.S. This is one of the basic example, and should be changed to fit your requirements

December 2, 2011

Using jQuery to append URL with anchor #

Question by jeffkee

While retrieving items via AJAX etc., the links are set to do this:

$(this).click(function(){
ajax_function(); // hypothetical ajax call that puts data into a <div> of my choice
return false;
});

In this scenario, I’m wondering what the best way is to utilize jQuery to add things to my URL in this format:

http://www.mydomain.com/products

turns into

http://www.mydamain.com/products#category-12,page-4

Further note- I’m talking about the actual URL of the browser (in the URL bar) not a URL that is part of the DOM.

Answer by jeffkee

After searching some more (with better keywords than before on Google) I found the answers I needed, and I wrote the following functions:

    function get_url_hash(argument) {
    var hash = location.hash.replace('#','');
    if(argument=='' || argument==undefined) {
        return hash;
        alert(blank);
    } else {
        var foundhash = false;
        // specific argument given - let's find the value attached. 
        var hashblock = hash.split(',');
        for(x in hashblock) {
            var hasharray = hashblock[x].split('-');
            if(hasharray[0]==argument) {
                return hasharray[1];
                foundhash = true;
            }
        }
        if(foundhash==false) {
            return false;
        }
    }
}

function modify_url_hash(argument, value) {
    // This function goes through the entire hash,
    // figures out which parts of the hash should be added, updated or removed based on entry, 
    // and then spits out final result. 
    var hash = get_url_hash();
    var foundhash = false; // foundhash is set to false by default. if this hash is NOT found, then we add it at the end! 
    var hashcount = 0; // keep count of total # so as to determine where to put the commas etc. 
    var newhash = '';
    if(hash.length>0) {
        var hashblock = hash.split(',');
        for(x in hashblock) {
            var hasharray = hashblock[x].split('-');
            if(hasharray[0]==argument) {
                hasharray[1] = value;
                foundhash = true;
            }

            if(hasharray[1]!=false && hasharray[1]!='') { // if new value is NOT false, we keep it in.. otherwise don't feed it to newhas so it disappears.
                if(hashcount>0) { newhash = newhash+','; }
                newhash = newhash+hasharray[0]+'-'+hasharray[1];
                hashcount++;
            }

        }
    }

    if(foundhash==false) {
        // this is a new hash block. 
        if(hashcount>0) { newhash = newhash+','; }
        newhash = newhash+argument+'-'+value;
    }
    location.hash = newhash;
}

Answer by Starx

May be something like

oldlink = $('#mylinkselector').attr("href");
newlink = oldlink="#category-12,page-4";
$("#mylinkselector").prop("href",newlink);
May 13, 2011

How to use jquery rewrite redirect url

Question by user610983

I created a drop down list(with javascript onchange features), I would like to rewrite the redirect url from this: http://localhost/en/abc/kk.php?CT=1 to http://localhost/abc/kk.php?lang=en&CT=1 by using jquery.

Possible to do it?

Answer by Starx

You cannot rewrite the redirect url through clientside script such as javascript itself. You need .htaccess file to do so.

However, if the urls http://localhost/en/abc/kk.php?CT=1 is already present in your markup like in anchor tags

<a href="http://localhost/en/abc/kk.php?CT=1">Some Link Text</a>

Then you can use jQuery to change the value

$(document).ready(function() {
    $("a").attr("href","http://localhost/abc/kk.php?lang=en&CT=1");
    //It is better to replace the values using pattern mathcing
});
...

Please fill the form - I will response as fast as I can!