...

View Full Version : 'header "Location: URL"' ...load once but never again. Need a bit of help.



new_to_this
05-28-2011, 11:05 AM
Hi

I am making code for my site.

It is meant to load a page "path/file.html" if the php query returns true.

If the login is within seven days the "path/file.html" page does not load. If they login equal to or greater than 7 days then the login integer is recorded in increments of +1. Then the true statement calls the header('Location: http://www.my_domain.co/path/file.html'); code. Or the next block will call instead.

I am unsure if this code: if($U-$join_stamp>=604800); will work to how expected. The 'U' is the time of current login from epoch, and the join stamp is time of member joining at the epoch. So the 604800 seconds is 7 days, and if its passed the else is meant to work.

Plus i am unsure if i have the if and else statements at the right place. Actually im even unsure if they are right at all. Do i need to do ifelse statements?

Please could someone tell me if this will work, i'm still trying to work out how to configure MySql and this bit of code here is not tested yet.

The code is here:



<?php
$con = mysql_connect("","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("db_name", $con);

$result = mysql_query("SELECT * FROM profile");

while($row = mysql_fetch_array($result)) {
if($U-$join_stamp>=604800);
else {
$sql2="UPDATE skadate_profile SET login=login+1";
mysql_query($sql);
}
}
}
if($result['login']>=1) {
$TooManylogin="Yes"
if($username===$result['username'] && $password===$result['password']) {
if($TooManylogin==="Yes") {
else {
echo "never redirect again";
}
}
}
if($result['login']==0) {
$TooManylogin="No"
if($username===$result['username'] && $password===$result['password']) {
if($TooManylogin==="No") {
else {
header('Location: http://www.my_domain.co/path/file.html');
}
}
}
}
}

mysql_close($con);

thanks
steve

new_to_this
05-28-2011, 12:10 PM
Hi

code has been edited due to updates

Fou-Lu
05-29-2011, 07:31 PM
This isn't a MySQL problem, moving to PHP.
This is always true:


if($U-$join_stamp>=604800);

Any semi-colon at the end of a branch is always true once and only once. I'm not sure what you're doing with it though since you don't have any body for it.

The fetching is horrendously inefficient. It is iterating every record in the database to later compare against a single $username/$password (which has not actually been provided anywhere....) field.

It also has a number of syntax errors just looking at it. You have else's all over the place without corresponding braces to control them, more closing braces than opening, and semi-colons missing from expressions and assignments. It looks like you have simply closed braces to try and combat syntactical errors, and this will generate a number of logical flaws as well (especially with the unusual usage of the else's scattered around).

new_to_this
05-29-2011, 08:18 PM
Hi
OK thanks for that, i appreciate it.

new_to_this
05-29-2011, 10:27 PM
Hi

Ok, i've edited the code...

Could somebody please check the syntax so i can know i'm headed in the right direction.

Here the new code:




while($dede = mysql_fetch_array($result, MYSQL_ASSOC))
{
printf($column["login"], $column["time_stamp"], $column["join_stamp"]);

{
if ($dede['time_stamp'-'join_stamp'])<=1);

{
if ($dede['login']>=3);
$TooManylogin="Yes";

{
if ($TooManylogin="Yes");
$sql = mysql_query("UPDATE table SET login=login+1") or die(mysql_error());

{
elseif ($dede['login']<=2);
$TooManylogin="No";

{
else ($TooManylogin="No");
$sql = mysql_query("UPDATE table SET login=login+1") or die(mysql_error());

{
if ($dede['login']==1);
header('Location: http://www.this_site.co/images/image-1.jpg');

{
else ($dede['login']==0);
header('Location: http://www.this_site.co/images/image-1.jpg');
}}}}}}}
}



thanks
steve

Fou-Lu
05-30-2011, 01:17 PM
This still suffers from the same branch problem. Perhaps I should clarify:


if (false);
{
print 'Hello world';
}

That will always print 'Hello World'.

Random bracing is also acceptable in PHP (and many other languages) as long as it has proper start and ends. I don't recommend doing such though.

It still looks to me like you're force bracing at the end to combat syntax errors (which will not work, I'd expect both the elseif and the else to throw T_IF and T_ELSE errors). You'll also need to evaluate your records, I highly doubt you have a column that looks like this: $dede['time_stamp'-'join_stamp']. Since you can technically subtract strings in PHP (which results in 0), that will default to $dede[0] if there is any applicable offset under 0 (which there is not in this since you are using MYSQL_ASSOC, so its returned result is null).

Start fixing by ALWAYS using braces in every branch situation. There is no measurable loss of using:


if (true)
{
print 'true';
}
else
{
print 'false';
}
// versus
if (true)
print 'true';
else
print 'false';

Since unbraced branch evaluations only take the first line of expression that follows, using braces will greatly reduce the probability of logic errors.

new_to_this
05-31-2011, 12:31 PM
Hi

Thanks very much for your posts. And thanks Fou-Lu.

I am absolute beginner coder in php, and your help is appreciated. Please forgive me for any ignorance.

I have reviewed the code, and please can someone view the syntax and comment.



if (($dede(['time_stamp']-['join_stamp'])==0) && ($dede(['time_stamp']-['join_stamp'])==1));
{
$overOneDays="Yes";
}

if ($dede(['time_stamp']-['join_stamp'])>=2);
{
$overOneDays="No";
}

if (($dede['login']==0) && ($dede['login']==1));
{
$TooManylogin="Yes";
}

if ($dede['login']>=2);
{
$TooManylogin="No";
}

if ($TooManylogin="No")
}
$sql = mysql_query("UPDATE table SET login=login+1") or die(mysql_error());
{

else
{
$sql = mysql_query("UPDATE table SET login=login+1") or die(mysql_error());
}

if (($TooManylogin="Yes") && ($overOneDays="Yes"));
{
header('Location: http://www.the_site.co/images/image-1.jpg');
}


thanks
steve

new_to_this
06-02-2011, 08:19 PM
Sorry, this code again in
tags.




if (($dede(['time_stamp']-['join_stamp'])==0) && ($dede(['time_stamp']-['join_stamp'])==1));
{
$overOneDays="Yes";
}

if ($dede(['time_stamp']-['join_stamp'])>=2);
{
$overOneDays="No";
}

if (($dede['login']==0) && ($dede['login']==1));
{
$TooManylogin="Yes";
}

if ($dede['login']>=2);
{
$TooManylogin="No";
}

if ($TooManylogin="No")
}
$sql = mysql_query("UPDATE table SET login=login+1") or die(mysql_error());
{

else
{
$sql = mysql_query("UPDATE table SET login=login+1") or die(mysql_error());
}

if (($TooManylogin="Yes") && ($overOneDays="Yes"));
{
header('Location: http://www.the_site.co/images/image-1.jpg');
}

Fou-Lu
06-02-2011, 09:16 PM
This is always true:


if($U-$join_stamp>=604800);




This still suffers from the same branch problem. Perhaps I should clarify:


if (false);
{
print 'Hello world';
}

That will always print 'Hello World'.


On top of the above, you now have variable functions specified by a variable called $dede. The function signature will be int (*fp)(int);. Assuming that $dede is supposed to still be an array and not a function pointer, than the following problem still exists with that:


...I highly doubt you have a column that looks like this: $dede['time_stamp'-'join_stamp']. Since you can technically subtract strings in PHP (which results in 0), that will default to $dede[0] if there is any applicable offset under 0 (which there is not in this since you are using MYSQL_ASSOC, so its returned result is null)..
The only difference is that instead you are retrieving a string, and then subtracting another string (resulting in 0 anyway).

This is wrong:


if ($TooManylogin="No")

That is an assignment, not a comparison. Assignments will only fail if you run out of memory to use, and that would terminate the script anyways. Otherwise, they are always true and will reflect as true when used in a comparison.

There are still brace issues as well. There are closes without opens, and opens leading to else's off of the same branch.

There are some parts of the manual you need to read over.
http://php.ca/manual/en/language.variables.basics.php
http://php.ca/manual/en/language.expressions.php
http://php.ca/manual/en/language.operators.assignment.php
http://php.ca/manual/en/language.operators.comparison.php
http://php.ca/manual/en/language.control-structures.php
http://php.ca/manual/en/functions.variable-functions.php

new_to_this
06-18-2011, 09:31 AM
Hi all

Wow there's some pretty complex stuff in the manual.

Please could someone give a few suggestions on the best method of coding for my header (Location: 'URL'); to open when needed.

A bit of detail about my project is here:


member joins at site
after 7 days, a Light Box window will load
use a database for sign up, and seven days, then less than or equal to 2 logins


So i need to work out the code to use.

I thought i'm on the right track so far, but help is needed...

Do i use functions? and arrays? Is there anything else i might use?

So far i have thought to talk to the database and recognise how many days after sign up, then at 7 days it again recognizes 0 - 2 logins and opens the Light Box.

Any hints on what i should use in the php code is extremely helpful, as my understanding of this code is limited.

Please provide hints, and help.

thanks
sc



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum