Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Do you know this trick?
Old 11-18-2008, 04:13 AM Do you know this trick?
Junior Talker

Posts: 1
Name: Ashish Moon
Trades: 0
Hi

I want a particular link to display on my webpage (homepage) only on 2nd or subsequent visit by the visitor. (Presume that the visitor does not clear the cookies during these visits.)
This link should not be visible on first visit to the webpage (homepage).

Can anyone help?

Thanks!
GuruSan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-18-2008, 06:59 AM Re: Do you know this trick?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
PHP Code:
if (isset($_COOKIE['zzz'])) {
   echo 
'<a href="http://google.com/">Zzz</a>';
} else {
   
setcookie('zzz'1time()+3600*24*365'/''.domain.com');

__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 11-18-2008, 10:03 AM Re: Do you know this trick?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by mtishetsky View Post
PHP Code:
if (isset($_COOKIE['zzz'])) {
   echo 
'<a href="http://google.com/">Zzz</a> ';
} else {
   
setcookie('zzz'1time()+3600*24*365'/''.domain.com');

The only problem with that being that it won't work, seeing how setcookie() uses HTTP headers, which must be sent before any HTML is outputted. Meaning the logic for setting the cookie surely cannot be in the same place that you would intend to echo a link, seeing how the link has to be within the body of the document.

http://us.php.net/setcookie
Quote:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
However, something like this would work, at the very top of the page:
PHP Code:
if (isset($_COOKIE['zzz'])) {
   
$link_optional '<a href="http://google.com/">Zzz</a>';
} else {
   
setcookie('zzz'1time()+3600*24*365'/''.domain.com');

Then, down in the body of your document:
PHP Code:
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed sed risus. Vivamus viverra neque nec ante. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur felis sapien, pretium nec, consectetuer eget, commodo quis, eros.</p>
<p><?php echo $link_optional?>Nunc iaculis vestibulum leo. Suspendisse viverra. Quisque lorem turpis, ultrices vel, aliquam nec, fringilla sit amet, lacus.</p>
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 11-18-2008 at 10:12 AM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-20-2008, 11:49 AM Re: Do you know this trick?
Average Talker

Posts: 19
Trades: 0
Don't forget to test if $link_optional is set i.e.

PHP Code:
<p><?php if(isset($link_optional)) echo $link_optional?>Nunc iaculis vestibulum leo. Suspendisse viverra. Quisque lorem turpis, ultrices vel, aliquam nec, fringilla sit amet, lacus.</p>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Limotek is offline
Reply With Quote
View Public Profile Visit Limotek's homepage!
 
Old 11-20-2008, 12:30 PM Re: Do you know this trick?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by Limotek View Post
Don't forget to test if $link_optional is set i.e.

PHP Code:
<p><?php if(isset($link_optional)) echo $link_optional?>Nunc iaculis vestibulum leo. Suspendisse viverra. Quisque lorem turpis, ultrices vel, aliquam nec, fringilla sit amet, lacus.</p>
Completely unneeded. An error won't be generated if you attempt to echo a variable that hasn't been set, because PHP is a loosely typed language. It will simply do nothing.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-20-2008, 01:44 PM Re: Do you know this trick?
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
Completely unneeded. An error won't be generated if you attempt to echo a variable that hasn't been set, because PHP is a loosely typed language. It will simply do nothing.
Not quite correct. Depends on reporting level. An undefined variable would report as a notice.
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 11-20-2008, 01:53 PM Re: Do you know this trick?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by maxxximus View Post
Not quite correct. Depends on reporting level. An undefined variable would report as a notice.
Not sure I've ever seen an example of that. For one thing, all of the production servers that I work on have error reporting off, but even the debugging servers with error reporting on I've used have never protested against the countless unset variables that I echo in almost every script. It's just part of my style. Why waste a line just to make it harder to read?

Of course, there are ways a variable could be used, other than an echo, that could cause an error if there is an undefined.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 11-20-2008 at 01:55 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-23-2008, 10:52 PM Re: Do you know this trick?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Notices shouldn't be on on live servers anyway.

And, if you use output buffering then Mike's code would work.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 11-24-2008, 10:31 AM Re: Do you know this trick?
Average Talker

Posts: 19
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
Completely unneeded. An error won't be generated if you attempt to echo a variable that hasn't been set, because PHP is a loosely typed language. It will simply do nothing.
It doesn't return an error but can return a warning/notice depending on the server (e.g. fasthosts) which in turn can mess up the style of your page. I know because this is something I experienced myself and had to spend a lot of time correcting.

Just because PHP is a loosely typed language, it doesn't mean you have to adopt lazy coding.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Limotek is offline
Reply With Quote
View Public Profile Visit Limotek's homepage!
 
Old 11-24-2008, 02:04 PM Re: Do you know this trick?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Quote:
Originally Posted by Limotek View Post
It doesn't return an error but can return a warning/notice depending on the server (e.g. fasthosts) which in turn can mess up the style of your page. I know because this is something I experienced myself and had to spend a lot of time correcting.

Just because PHP is a loosely typed language, it doesn't mean you have to adopt lazy coding.
Take the time to learn a bit more about programming. If you don't want to "adopt lazy coding", then initialize your variables in the first place -- that's why you're getting the notice. You just patched the symptom, not solved the problem.

I hope I don't see you writing any PHP 4 on this, failing to use OOP, or not declaring variable types for objects in function declarations. You set yourself up as a pedantic coder, so be ready for the responsibility -- ostentation can be a dangerous policy.

EDIT

And, be very cautious about taking a host's policies as guidelines for good programming. Hosts substitute attitude for knowledge and you'll soon learn that many of them just don't know what they're doing very well. I can't tell you how many times I've experienced hosts whose first response to **** near any question is "We don't support 3rd party apps." when the problem was in their server configuration. I can't tell you how many hosts have had safe mode on on a live server. I can't tell you ... all the wacked out things that hosts do b/c they simply are ignorant of proper procedures. So, again, be very cautious.

You have much to learn young one. Stick around, drop the attitude, and learn a lot. This is a great forum.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE

Last edited by JeremyMiller; 11-24-2008 at 02:19 PM..
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 11-25-2008, 05:45 AM Re: Do you know this trick?
Experienced Talker

Posts: 48
Name: Pieter
Location: Holland
Trades: 0
U can try using remote server addr in php store it inside a database, and check if the ip exists. If so show link
elcosmo is offline
Reply With Quote
View Public Profile
 
Old 11-25-2008, 06:12 AM Re: Do you know this trick?
Average Talker

Posts: 19
Trades: 0
Quote:
Originally Posted by JeremyMiller View Post
Take the time to learn a bit more about programming. If you don't want to "adopt lazy coding", then initialize your variables in the first place -- that's why you're getting the notice. You just patched the symptom, not solved the problem.

I hope I don't see you writing any PHP 4 on this, failing to use OOP, or not declaring variable types for objects in function declarations. You set yourself up as a pedantic coder, so be ready for the responsibility -- ostentation can be a dangerous policy.

EDIT

And, be very cautious about taking a host's policies as guidelines for good programming. Hosts substitute attitude for knowledge and you'll soon learn that many of them just don't know what they're doing very well. I can't tell you how many times I've experienced hosts whose first response to **** near any question is "We don't support 3rd party apps." when the problem was in their server configuration. I can't tell you how many hosts have had safe mode on on a live server. I can't tell you ... all the wacked out things that hosts do b/c they simply are ignorant of proper procedures. So, again, be very cautious.

You have much to learn young one. Stick around, drop the attitude, and learn a lot. This is a great forum.
Hi Jeremy,

Firstly, apologies if you thought my second post was rude. That certainly wasn't my intention and I was simply trying to say that just because a programming language accepts bad code; it doesn't mean you should let your standards drop. An example of this could be proper error handling. Hope that clarifies it better.

The reason I posted about the checking if the variable is set is because it was something I experienced where my variables were initialised inside an if else statement. The error was actually a warning that the variable may not be initialised even though I knew it had been. I understand that this is all server specific and the OP may not experience the same but I was genuinely trying to help avoid a problem that they may experience. Without knowing what server setup they have, it would be wrong for me to assume that error reporting is on/off, etc therefore I figure the best way would be to produce code that would work in both instances.

I agree that this is a good forum. That's why I've signed up and am trying to contribute; so I can learn more. I don't profess to being an expert.

Anyway, to cut this short, we've obviously got off on the wrong foot but I'm here to learn like most others probably and I look forward to chatting with everyone here.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Limotek is offline
Reply With Quote
View Public Profile Visit Limotek's homepage!
 
Old 11-25-2008, 01:04 PM Re: Do you know this trick?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
As far as the OOP vs Procedural debate, that is a good one. I use both. I find, for small sites, procedural or function based scripts are faster to create than pure OOP applications, which require more organizing at the outset, though of course classes make medium size and larger applications much more manageable. Of course, I have a couple of pre-written classes that I use in pretty much everything.

As far as warning/notices messing things up, I can't say I've ever experienced that, so I don't know how to address it. Often, when I echo an unset or empty variable, it is actually a property of an object:

PHP Code:
<?=$content->html?>
I filter this output with a __get method in the class, which automatically fills it with data from the database, if it exists. I've also never experienced any problems with this. Of course, in this case, the properties are all declared, which is nice. For tiny stuff, I have absolutely no problem with "lazy coding", since there's not much that can go wrong in the global name-space.

As Jeremy will probably point out, I also used a short-tag. Bite me

@Limotek: I don't know anyone thinks you're being rude. Welcome to the forum.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 11-25-2008 at 01:09 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-25-2008, 07:06 PM Re: Do you know this trick?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Quote:
Originally Posted by Limotek View Post
The reason I posted about the checking if the variable is set is because it was something I experienced where my variables were initialised inside an if else statement. The error was actually a warning that the variable may not be initialised even though I knew it had been.
A perfect example as to why notices should not be displayed on a live server -- you had followed good programming practices but received a notice anyway. The added conditional expression requires yet another calculation, however, thereby making your code ever-so-slightly less efficient.
Quote:
Originally Posted by Limotek View Post
I understand that this is all server specific and the OP may not experience the same but I was genuinely trying to help avoid a problem that they may experience. Without knowing what server setup they have, it would be wrong for me to assume that error reporting is on/off, etc therefore I figure the best way would be to produce code that would work in both instances.
Fair enough. You'll find that on live servers many hosts hide error messages which causes a big pain (sometimes you simply can't access those logs on shared servers).

As for footing, it's all good. I took your comments as a dictum and subsequent judgement, but have seen way too much code to accept many dictums or the judgements which stem from them. My experience is that there are no programming rules (except syntax rules), but guidelines. For each such guideline there are times when the needs of the application outweigh the preferences of the guideline. For example, using OOP, which as Wayfarer pointed isn't always the best option -- PHP creates more overhead for objects and function calls than procedural programming, so the give and take means sometimes breaking the academically-preferred OOP.

Again, welcome to the forums. I truly believe these are the best forums out there.

Quote:
Originally Posted by wayfarer07 View Post
As Jeremy will probably point out, I also used a short-tag. Bite me
Bite, bite... lol. How dare you! I mean, what if you were trying to output <?xml version="1.0"?>? lol.

Now, looking at it a slightly different way, if you have 30 cases where you use short tags for echoing out a value, you go from <? to <?php echo each time for a difference of 8 characters per instance, 240 characters saved overall by using short tags, which is a decent savings of typing. I have no idea which is parsed by PHP more rapidly, but short tags do make coding a bit shorter.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 11-25-2008, 08:52 PM Re: Do you know this trick?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Quote:
Originally Posted by Limotek View Post
Firstly, apologies if you thought my second post was rude. That certainly wasn't my intention and I was simply trying to say that just because a programming language accepts bad code; it doesn't mean you should let your standards drop. An example of this could be proper error handling. Hope that clarifies it better.
If you were using .net, the VS IDE would highlight (actually underline in red, like a misspelling in Word) variables that are used before they're initialized. Taking into account multiple code paths.

Code:
int i;

if(j == 0)
   i = 3;
else if(j < 2)
   i = 50;

j = i;
Is a compile error. It's interesting to see the different philosophies giving rise to such different implementations. I guess a lot of open source people feel like an error like this should be ignored and swallowed. On the other side of the fence, they say the value of i is ambiguous in some code paths, would result in a completely random value under C++ rules, and that if a bad thing is possible (enough to be caught by the compiler) it should be addressed by a programmer.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 11-25-2008, 09:31 PM Re: Do you know this trick?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
RE: LearningNewbie's comments

I'm actually quite for a more strongly typed language. That PHP doesn't support a mechanism for memory management (like C++) is quite annoying. For me at least, if a language will deprive you of directory memory addressing capabilities and be weakly typed, then allowing features like (int)$x = (string)$y converting $x to a string is a nice feature that I'll eagerly take advantage of. As you know, in languages like C++ that operation simply wouldn't work b/c declaring $x as an int assigns only so many bytes while a string (array of chars) is many more bytes and could cause critical memory issues so it is imperative that they enforce types.

That said, can you imagine when PHP adds such support? With all the non-programmer programmers using PHP? Whoa!

But, I will truly love it b/c with memory addressing one can make things so much more efficient.

(Oh, to answer the implied question, I chose PHP b/c it was easy-to-use and highly available. )
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 11-26-2008, 04:43 AM Re: Do you know this trick?
Banned

Posts: 5
Trades: 0
thanks,I learned
euart is offline
Reply With Quote
View Public Profile
 
Old 11-26-2008, 02:22 PM Re: Do you know this trick?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Quote:
Originally Posted by JeremyMiller View Post
(Oh, to answer the implied question, I chose PHP b/c it was easy-to-use and highly available. )
Nah, actually, I had a different implication in mind. Somebody showed me Eclipse the other day. It's a Java compiler that the guy was using for Ruby and Perl. I don't know whether it does PHP as well. It wasn't bad at all, not Visual Studio, but still pretty impressive. Fully integrated with the compiler and such, but a lot of rich support. The guy was telling me that if you have a variable called i and another one called i_doubt_it, you can tell the IDE to find you all references to i, and it'll search your code, make a list, and not be confused by i_doubt_it, etc.

I really like uninitialized variables causing compile errors. Warnings aren't so useful, because people ignore them. Not being able to compile forces you to do the right thing, and, depending on the language (default values, like 0 for numerics, empty strings, etc, are another option) can save your neck.

To me, the implied question is what the right approach is. Whether it's better to invest the time up front, or when assumptions lead to bad things happening?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 11-26-2008, 05:22 PM Re: Do you know this trick?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Eclipse does have a PHP plugin. Over the last 2 years I've tried eclipse twice. Unfortunately it crashes way too much for me at this point.

Initializing variables is always a good idea. Take for instance

PHP Code:
if (isset($_COOKIE['zzz'])) {
   
$link_optional '<a href="http://google.com/">Zzz</a>';
} else {
   
setcookie('zzz'1time()+3600*24*365'/''.domain.com');

Cookies can be set by the user, so this is hackable and if you're on a poorly-configured host, with register globals on then $link_optional could also be initialized by the end user.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Do you know this trick?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.56332 seconds with 12 queries