Make <a> change href=" button with each new access on a given page

Asked

Viewed 424 times

5

I have a little doubt in a project that I’m developing.

Here’s the thing, I have two buttons on the page, like these ( identical at different positions ):

Top of the page:

<a target="_blank" class="btn-primary" href="http://localhost/exemplo/"> Clique Aqui </a>

End of page:

<a target="_blank" class="btn-primary" href="http://localhost/exemplo/"> Clique Aqui </a>

I need to make sure that each time the page containing these buttons is accessed, increase the number in the folder. Example:

A person accessed this page the first time: ( Note that the link contains /01/ )

http://localhost/exemplo/01/

When a another person accesses for the second time: ( Note that the link contains /02/ )

http://localhost/exemplo/02/

And so consecutively: (/03/, /04/, /05/, /06/, etc....)

Each new access increases the value on the link, increasing.

Also, is it possible to implement something that can tell me what number it is at the moment? I have no idea, maybe something like a file that is updated (txt maybe), telling me which number you stopped at.

  • 1

    I can show you a way to do it, it’s simple. But if possible, explain the reason for it. It doesn’t sound good to me. Before judging or even posting an answer, I would like to understand why.

  • I updated the question so there’d be no misunderstandings.

  • I didn’t understand the correlation of this with these numbers. It got more confused.

  • You are from Maranhão?

  • I am a friend @Sampaio Leal

  • Sorry @Daniel Omine but it wasn’t that "answer" I expected from you. I hope other users can answer this question.

  • I will try to edit this question in a more "community" way as you say.

  • Now let’s make the question cleaner by deleting these comments. :)

  • 1

    your idea is to know how many people accessed the page?

  • This would be just a friendly add-on @André Baill, the main thing is that at each access, increase that little number after the folder "/example/ to each new access. : ) It turns out that I would like to know which number stopped at, because then I would not need to access the page, and be +1 of those who accessed.

  • 1

    Make a file and write to txt, then each time, recovers what has +1

  • I edited my comment :) You could make a reply?

  • 1

    Do you use any development framework or php pure?

  • Pure PHP friend use @Kenny Rafael :D

  • In that case it takes more work...

  • 1

    You will have to create a route in . htaccess and save one JSON with the countdown to keep updating, I can’t develop any code now to help, but the way is this...

  • I see something so simple. It makes of accounts that it is an HTML page in which has this button that has a link that needs to be changed with each new access, as I put in the question :)

  • Please avoid long discussions in the comments; your talk was moved to the chat

Show 13 more comments

1 answer

3


You can simply use the .htaccess so that whenever the link is clicked, the page passes the value to PHP:

RewriteRule ^exemplo/([0-9]*)/$ index.php?contador=$1 [QSA,L]

Then in the index.php just increase this counter by 1 and print the link:

$contador = $_GET['contador'] + 1;
echo "<a target='_blank' class='btn-primary' href='http://localhost/exemplo/".$contador."/'> Clique Aqui </a>";

Or you can use the file read when loading the page:

$file = 'exemplo.txt'; 

$atual = file_get_contents($file); 

$contador = $atual + 1 ;

echo "<a target='_blank' class='btn-primary' href='http://localhost/exemplo/".$contador."/'> Clique Aqui </a>";

Finally, to save to a file you can use the function file_put_contents($file, $string);, for example:

$file     = 'exemplo.txt'; 
file_put_contents($file, $contador);

Note: Please note that you are using this, you may be using it wrong.

  • Jorge, you can make it so that instead of adding +1 each time you reload it, you add +1 to each new ip (each new visitor), instead of every reload ?

  • @Alexandrelopes there is, but that was another question. You needed to save the IP’s and confirm every time you upload the page or log in if that’s the case, and then update Count. But as I tell you, this gives a new question. If you want to ask a new question...

  • Thanks for the reply to the comment my friend Jorge! : D - I will ask a new question :D

  • Jorge, here’s the question: http://answall.com/questions/167270/fazer-bot%C3%a3o-a-alterar-href-a-cada-novo-visitante-ip-em-determinada-p%C3%a1g

  • @Alexandrelopes I have not had time, when I can answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.